Qt或libcurl c ++:Facebook api POST请求图像上传

时间:2016-03-06 21:38:05

标签: c++ facebook qt http post

美好的一天。我不能强调如何创建POST请求来上传图像。我在网上搜索,发现了几个版本。但总是对我的回应(在请求之后) - “(#324)需要上传文件”。 谢谢!

ui->webView->load(QUrl("https://www.facebook.com/dialog/oauth?"
                       "client_id=XXXXXXXXXX&"
                       "display=popup&"
                       "response_type=token&"
                       "scope=user_status,user_photos,publish_actions&"
                       "redirect_uri=https://www.facebook.com/connect/login_success.html"));

1

QString uploadUrl = "https://graph.facebook.com/me/photos?access_token=" +
                    accessToken;

QString photoPath = QCoreApplication::applicationDirPath() + "/image_example.png";

QFileInfo fileInfo(photoPath);
QFile file(photoPath);

QString comment = "User provided message";

QString bound="---------------------------17673466415141";

QByteArray data;

data.append("--" + bound + "\r\n");
data.append("Content-Disposition: form-data; name=\"access_token\"\r\n\r\n");
data.append(accessToken + "\r\n");

data.append("--" + bound + "\r\n");
data.append("Content-Disposition: form-data; name=\"source\"; filename=\""
            + file.fileName()+"\"\r\n");
data.append("Content-Type: image/" + fileInfo.suffix().toLower() + "\r\n\r\n");
data.append(file.readAll());
data.append("\r\n");

data.append("--" + bound + "\r\n");
data.append("Content-Disposition: form-data; name=\"message\"\r\n\r\n");
data.append(comment);
data.append("--" + bound + "\r\n");

QNetworkRequest request(uploadUrl);

request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
request.setRawHeader("Accept-Language", "en-us,en;q=0.5");
request.setRawHeader("Accept-Encoding", "gzip,deflate");
request.setRawHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
request.setRawHeader("Keep-Alive", "300");

request.setRawHeader("Content-Type",
                     QString("multipart/form-data; boundary=" + bound).toLatin1());
request.setRawHeader("Content-Length", QString(data.size()).toLatin1());

QNetworkAccessManager * manager = new QNetworkAccessManager(this);
QNetworkReply * reply =  manager->post(request, data);
QEventLoop wait;

connect(manager, SIGNAL(finished(QNetworkReply*)), &wait, SLOT(quit()));
connect(manager, SIGNAL(finished(QNetworkReply*)), manager, SLOT(deleteLater()));

QTimer::singleShot(10000, &wait, SLOT(quit()));
wait.exec();

QByteArray answer = reply->readAll();
reply->deleteLater();

qDebug() << answer;

2

QString uploadUrl = "https://graph.facebook.com/me/photos?access_token=" +
                    accessToken;

QString photoPath = QCoreApplication::applicationDirPath() + "/image_example.png";

QFileInfo fileInfo(photoPath);
QFile file(photoPath);

QPixmap pixmap;
pixmap.load(photoPath);
QByteArray data;

QBuffer buffer(&data);
buffer.open(QIODevice::ReadWrite);
pixmap.save(&buffer, "png");
buffer.close();

QHttpMultiPart * multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));

imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"source\"; filename=\""+ file.fileName()));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"message\""));

imagePart.setBody(data);
multiPart->append(imagePart);

QNetworkAccessManager * manager = new QNetworkAccessManager(this);
QNetworkReply * reply =  manager->post(request, multiPart);
QEventLoop wait;

connect(manager, SIGNAL(finished(QNetworkReply*)), &wait, SLOT(quit()));
connect(manager, SIGNAL(finished(QNetworkReply*)), manager, SLOT(deleteLater()));

QTimer::singleShot(10000, &wait, SLOT(quit()));
wait.exec();

QByteArray answer = reply->readAll();
reply->deleteLater();

qDebug() << answer;

1 个答案:

答案 0 :(得分:0)

我实际上正在使用类似于你的第二个例子的东西。但你需要:

1 /在imagePart标题中设置图像的大小
2 /您正在重新定义ContentDisposition。你只需要第一个。

所以回顾一下:

QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(type.name()));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"source\"; filename=\""+ file.fileName()));
imagePart.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(fileInfo.size()).toLocal8Bit());
imagePart.setBody(file.readAll());
multiPart->append(imagePart);