pipacs / o2

OAuth 2.0 for Qt
BSD 2-Clause "Simplified" License
317 stars 147 forks source link

Upload photo to flickr #94

Closed cristeab closed 7 years ago

cristeab commented 7 years ago

Hi

I am trying to use o2 library to upload photos to flickr. Authentication process goes well, but I am unable to upload a photo. The error says: "Error when uploading file: authentication required"

I am sending a photo with

manager_ = new QNetworkAccessManager(this);
 o1_ = new O1Flickr(this);
 requestor_ = new O1Requestor(manager_, o1_, this)
QNetworkRequest request(uploadUrl_);
request.setHeader(QNetworkRequest::ContentTypeHeader, CONTENT_TYPE);
request.setHeader(QNetworkRequest::ContentLengthHeader, postData.length());
QNetworkReply *reply = requestor_->post(request, QList<O0RequestParameter>(), postData);

postData is a QbyteArray constructed as shown below:

    requestData += QString("--" HTTP_PART_BOUNDARY "\r\n").toUtf8();
    requestData += "Content-Disposition: form-data; name=\"title\"\r\n";
    requestData += "\r\n"+title_+"\r\n";

    requestData += QString("--" HTTP_PART_BOUNDARY "\r\n").toUtf8();
    requestData += "Content-Disposition: form-data; name=\"description\"\r\n";
    requestData += "\r\n"+description_+"\r\n";

    requestData += QString("--" HTTP_PART_BOUNDARY "\r\n").toUtf8();
    requestData += "Content-Disposition: form-data; name=\"tags\"\r\n";
    requestData += "\r\n"+tags_+"\r\n";

    requestData += QString("--" HTTP_PART_BOUNDARY "\r\n").toUtf8();
    requestData += "Content-Disposition: form-data; name=\"is_public\"\r\n";
    requestData += "\r\n"+isPublic_+"\r\n";

    requestData += QString("--" HTTP_PART_BOUNDARY "\r\n").toUtf8();
    requestData += "Content-Disposition: form-data; name=\"is_friend\"\r\n";
    requestData += "\r\n"+isFriend_+"\r\n";

    requestData += QString("--" HTTP_PART_BOUNDARY "\r\n").toUtf8();
    requestData += "Content-Disposition: form-data; name=\"is_family\"\r\n";
    requestData += "\r\n"+isFamily_+"\r\n";

    requestData += QString("--" HTTP_PART_BOUNDARY "\r\n").toUtf8();
    requestData += "Content-Disposition: form-data; name=\"photo\"; filename=\""+
            fileInfo.fileName()+"\"\r\n";
    QMimeDatabase db;
    QMimeType mimeType = db.mimeTypeForFile(filename);
    requestData += "Content-Type: "+mimeType.name()+"\r\n";
    requestData += "\r\n"+file.readAll()+"\r\n";
    file.close();

    requestData += QString("--" HTTP_PART_BOUNDARY "--\r\n").toUtf8();

Could someone tell me what I am doing wrong ?

thanks Bogdan

cristeab commented 7 years ago

I have found the problem: when building the request data only 'photo' should be put there, all other params are optional and should be signed.

So postData should be constructed as

requestData += QString("--" HTTP_PART_BOUNDARY "\r\n").toUtf8();
requestData += "Content-Disposition: form-data; name=\"photo\"; filename=\""+
            fileInfo.fileName()+"\"\r\n";
 QMimeDatabase db;
 QMimeType mimeType = db.mimeTypeForFile(filename);
 requestData += "Content-Type: "+mimeType.name()+"\r\n";
 requestData += "\r\n"+file.readAll()+"\r\n";
 file.close();

 requestData += QString("--" HTTP_PART_BOUNDARY "--\r\n").toUtf8();