Название: Facebook api
Отправлено: Disa от Март 06, 2016, 04:11
Добрый день. Никак не соображу как правильно оформить POST запрос для того чтобы загрузить изображение. Искал по сети, перебрал несколько вариантов, пока все время выдает в ответ на запрос "(#324) Requires upload file". Вот авторизация: 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"));
Вот какие варианты перебрал I. 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; II 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;
Буду рад любой помощи. Спасибо
|