QNetworkAccessManager post to url: Error downloading https://api.instagram.com/oauth/access_token - server replied: BAD REQUEST

  • Hello guys, I'm trying to write an Instagram tool, but I've encountered a problem with QNetworkAccessManager. Even though, the same details ran with curl in CLI it throws some information, while using Qt I always get the error in the title. Am I doing smth wrong in the post script? I followed a dozen examples which were all saying the same thing.

    @curl -F 'client_id=d01483900e3949c194ff6a73910886ba' -F 'client_secret=6d43805a777645ca9d7e96416d4fb83d' -F 'grant_type=authorization_code' -F 'redirect_uri=http://instagram.com' -F 'code=9e1aa67677c64b23abe1bb02fc81df96' https://api.instagram.com/oauth/access_token@

    @void MainWindow::_loadWebAuthToken()
    this->_debug("_loadWebAuthToken: TOKEN OBTAIN");

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(_loadWebAuthTokenReplyFinish(QNetworkReply*)));
    QUrl url;
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    QByteArray postData;
    QNetworkReply *reply = manager->post(request, postData);


    void MainWindow::_loadWebAuthTokenReplyFinish(QNetworkReply *reply)

    if(reply->error() == QNetworkReply::NoError) {
        // Get the http status code
        int v = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        if (v >= 200 && v < 300) { // Success
            // Here we got the final reply
            QString replyText(reply->readAll());
            this->_debug("{" + replyText + "}");
        } else if (v >= 300 && v < 400) {  // Redirection
            // Get the redirection url
            QUrl newUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
            // Because the redirection url can be relative,
            // we have to use the previous one to resolve it
            newUrl = reply->url().resolved(newUrl);
            QNetworkAccessManager *manager = reply->manager();
            QNetworkRequest redirection(newUrl);
            QNetworkReply *newReply = manager->get(redirection);
            connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(_loadWebAuthTokenReplyFinish(QNetworkReply*)));
            return; // to keep the manager for the next request
    } else {
        // Error
        QString error(reply->errorString());


  • curl "-F" options will cause the request to be sent Content-Type "multipart/form-data":http://tools.ietf.org/html/rfc2388 and your Qt code is using application/x-www-form-urlencoded. If Instagram is expecting the multipart format then you should look at QHttpMultiPart

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.