Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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;
    url.setUrl("https://api.instagram.com/oauth/access_token");
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    
    QByteArray postData;
    postData.append("client_id=d01483900e3949c194ff6a73910886ba")
            .append("&client_secret=6d43805a777645ca9d7e96416d4fb83d")
            .append("&grant_type=authorization_code")
            .append("&redirect_uri=http://instagram.com")
            .append("&code=9e1aa67677c64b23abe1bb02fc81df96");
    
    QNetworkReply *reply = manager->post(request, postData);
    

    }

    void MainWindow::_loadWebAuthTokenReplyFinish(QNetworkReply *reply)
    {
    reply->deleteLater();

    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
            this->_debug("::reply");
            QString replyText(reply->readAll());
            this->_debug("{" + replyText + "}");
        } else if (v >= 300 && v < 400) {  // Redirection
            // Get the redirection url
            this->_debug("::redirect");
            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
        this->_debug("::error");
        QString error(reply->errorString());
        this->_debug(error);
    }
    
    reply->manager()->deleteLater();
    

    }@



  • 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



  • It looks like a problem with the URL-encoded code that you're exchanging for a login token. I wrote about authenticating a Qt app with Google SSO, and there I describe how to intercept the login parameters and URL-decode them before Qt continues with the flow.



  • @ihaveajob

    You think after 7 1/2 years OP is still looking for help in this case? ;-)



  • @ihaveajob I see that you have published in several old posts a similar message that seems spam to me, some moderator can analyze this behavior


Log in to reply