Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkAccessManager post to url: Error downloading https://api.instagram.com/oauth/access_token - server replied: BAD REQUEST
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 6 Posters 4.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gridorian
    wrote on last edited by
    #1

    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();
    

    }@

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      1
      • I Offline
        I Offline
        ihaveajob
        wrote on last edited by
        #3

        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.

        Pl45m4P eyllanescE 2 Replies Last reply
        0
        • I ihaveajob

          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.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @ihaveajob

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


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0
          • I ihaveajob

            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.

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #5

            @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

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            1 Reply Last reply
            1
            • L Offline
              L Offline
              Leonel Stanly
              Banned
              wrote on last edited by Leonel Stanly
              #6
              This post is deleted!
              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved