Send an experimental file to the experimental album on Facebook

And I had to create the Http header by hand because I couldn't
figure out how to do using HttpMultiPart from Qt.
not fun.

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-12-25 12:55:06 -02:00 committed by Dirk Hohndel
parent 83fd196c4a
commit 3d3eac7cc7
3 changed files with 47 additions and 0 deletions

View file

@ -27,6 +27,7 @@
#include <QFileDialog>
#include <string>
#include <iostream>
#include <QHttpMultiPart>
#include "socialnetworks.h"
#include "../qthelper.h"
@ -844,6 +845,7 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event)
void DiveListView::publishFacebook()
{
FacebookManager *fb = FacebookManager::instance();
fb->sendDive(1);
}
void DiveListView::shiftTimes()

View file

@ -8,7 +8,9 @@
#include <QNetworkAccessManager>
#include <QUrlQuery>
#include <QEventLoop>
#include <QHttpMultiPart>
#include <QSettings>
#include <QFile>
#include <QDebug>
#include "pref.h"
@ -180,3 +182,45 @@ void FacebookManager::setDesiredAlbumName(const QString& a)
{
albumName = a;
}
/*
*/
void FacebookManager::sendDive(int divenr)
{
QUrl url("https://graph.facebook.com/v2.2/" + QString(prefs.facebook.album_id) + "/photos?" +
"&access_token=" + QString(prefs.facebook.access_token) +
"&source=image");
QNetworkAccessManager *am = new QNetworkAccessManager(this);
QFile file("subsurfaceuploadtest.png");
file.open(QIODevice::ReadOnly);
QNetworkRequest request(url);
QString bound="margin";
//according to rfc 1867 we need to put this string here:
QByteArray data(QString("--" + bound + "\r\n").toLocal8Bit());
data.append("Content-Disposition: form-data; name=\"action\"\r\n\r\n");
data.append("https://graph.facebook.com/v2.2/\r\n");
data.append("--" + bound + "\r\n"); //according to rfc 1867
data.append("Content-Disposition: form-data; name=\"uploaded\"; filename=\"subsurfaceuploadtest.png\"\r\n"); //name of the input is "uploaded" in my form, next one is a file name.
data.append("Content-Type: image/jpeg\r\n\r\n"); //data type
data.append(file.readAll()); //let's read the file
data.append("\r\n");
data.append("--" + bound + "--\r\n"); //closing boundary according to rfc 1867
request.setRawHeader(QString("Content-Type").toLocal8Bit(),QString("multipart/form-data; boundary=" + bound).toLocal8Bit());
request.setRawHeader(QString("Content-Length").toLocal8Bit(), QString::number(data.length()).toLocal8Bit());
QNetworkReply *reply = am->post(request,data);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
qDebug() << reply->readAll();
}

View file

@ -22,6 +22,7 @@ public slots:
void tryLogin(const QUrl& loginResponse);
void logout();
void setDesiredAlbumName(const QString& albumName);
void sendDive(int divenr);
private:
explicit FacebookManager(QObject *parent = 0);