pipacs / o2

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

Twitter media/upload end point produces an error saying : code 32 Could not authenticate you. #118

Closed TTalaat closed 6 years ago

TTalaat commented 6 years ago

Hi I am trying to upload photos to twitter from my app and i am using your library. The following is the code that i am using:

QFile file(imgPath);
if (!file.open(QIODevice::ReadOnly))
    {
        return false;
    }
QByteArray m_buffer = file.readAll();

O1Requestor* requestor = new O1Requestor(d->netMngr, d->o1Twitter, this);
QList<O0RequestParameter> reqParams = QList<O0RequestParameter>();

reqParams << O0RequestParameter(QByteArray("media"), m_buffer);
reqParams << O0RequestParameter(QByteArray("media_type"), QByteArray("image/jpeg"));

QByteArray postData = O1::createQueryParameters(reqParams);

QUrl url = QUrl("https://upload.twitter.com/1.1/media/upload.json");
QNetworkRequest request(url);

request.setHeader(QNetworkRequest::ContentTypeHeader, O2_MIME_TYPE_XFORM);
QNetworkReply *reply = requestor->post(request, reqParams, postData);

The result is the following:

ERROR: "Host requires authentication" Content: "{\"errors\":[{\"code\":32,\"message\":\"Could not authenticate you.\"}]}"

I tried doing something simple like posting a tweet saying Hello to test the authentication using statuses/update endpoint and it worked with no errors. The following is the code also if it is required:

QUrl url2 = QUrl("https://api.twitter.com/1.1/statuses/update.json);

reqParams = QList<O0RequestParameter>();
reqParams << O0RequestParameter(QByteArray("status"), "Hello");
postData = O1::createQueryParameters(reqParams);

request.setUrl(url2);
request.setHeader(QNetworkRequest::ContentTypeHeader, O2_MIME_TYPE_XFORM);
QNetworkReply *reply = requestor->post(request, reqParams, postData);

Could you please help me with this issue. Thanks

pipacs commented 6 years ago

If authentication works in some cases only, I'd check the app permissions you have configured within your Twitter app settings: https://developer.twitter.com/en/docs/basics/authentication/overview/application-permission-model

TTalaat commented 6 years ago

It is set to read and write..this is the required in case of media upload correct?