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. Dropbox QOauth module issue
Forum Updated to NodeBB v4.3 + New Features

Dropbox QOauth module issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 190 Views 1 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.
  • Y Offline
    Y Offline
    yanmo
    wrote on 30 Oct 2023, 22:11 last edited by
    #1

    Hi,

    I am trying to connect to dropbox usin QOauth Api with Qt 6.5. I do this code:

    If i have the line : " (*parameters).remove(QStringLiteral("redirect_uri"));"
    it open the browser, I need to accept and I have the code to paste to connect available on the webpage (but not in my application).
    If I comment this line, I have the following issue on the web browser :
    "
    Error (400)
    It seems the app you were using submitted a bad request. If you would like to report this error to the app's developer, include the information below.

    More details for developers
    Invalid redirect_uri. It must exactly match one of the redirect URIs you've pre-configured for your app (including the path)
    "

    my port match with the one on the dropbox app redirect uri "http://127.0.0.1:56207"
    I don't know what to do to succeed to connect to Dropbox API.

    m_auth = new QOAuth2AuthorizationCodeFlow(this);
    
    QOAuthHttpServerReplyHandler* replyHandler = new QOAuthHttpServerReplyHandler(56207,this);
    m_auth->setReplyHandler(replyHandler);
    
    m_auth->setAuthorizationUrl(QUrl("https://www.dropbox.com/oauth2/authorize"));
    m_auth->setAccessTokenUrl(QUrl("https://api.dropboxapi.com/oauth2/token"));
    m_auth->setClientIdentifier(m_apiKey);
    m_auth->setClientIdentifierSharedKey(m_apiSecret);
    m_auth->setNetworkAccessManager(new QNetworkAccessManager(this));
    
    m_auth->setModifyParametersFunction([](QAbstractOAuth::Stage stage,  QMultiMap<QString, QVariant>* parameters) {
        if (QOAuth2AuthorizationCodeFlow::Stage::RequestingAuthorization == stage || QOAuth2AuthorizationCodeFlow::Stage::RequestingAccessToken   == stage)
        {
            (*parameters).remove(QStringLiteral("redirect_uri"));
        }
        
    });
    
    if(haveValidToken()){
        return;
    }
    
    connect(m_auth, &QAbstractOAuth::requestFailed, [=](){
        qDebug()<<"API Key or API Secret or port incorrect";
    });
    
    connect(m_auth, &QAbstractOAuth::authorizeWithBrowser, [=](QUrl url) {
        QUrlQuery query(url);
        
        query.addQueryItem("force_reapprove", "true"); // Param required to get data everytime
        query.addQueryItem("token_access_type", "offline"); // Needed for Refresh Token (as AccessToken expires shortly)
        query.addQueryItem("reponse_type","code");
        //  query.addQueryItem("force_reauthentication","false");
        
        url.setQuery(query);
        QDesktopServices::openUrl(url);
        //        m_browser.resize(900, 700);
        //        m_browser.page()->profile()->setPersistentCookiesPolicy(QWebEngineProfile::AllowPersistentCookies);
        //        m_browser.load(url);
    });
    
    connect(m_auth, &QAbstractOAuth2::authorizationCallbackReceived,[=](const QVariantMap data)
            {
                if (false == data.isEmpty())
                {
                    QString authCode = data.value("code").toString();
                    qDebug() << "auth code : " << authCode;
                    if(!authCode.isEmpty())
                    {
                        QUrl url(m_auth->accessTokenUrl());
                        
                        QUrlQuery queryUrl(url.query());
                        queryUrl.addQueryItem("code",authCode);
                        queryUrl.addQueryItem("grant_type","authorization_code");
                        queryUrl.addQueryItem("client_id",m_apiKey);
                        queryUrl.addQueryItem("client_secret",m_apiSecret);
                        queryUrl.addQueryItem("redirect_uri",m_auth->replyHandler()->callback());
                        url.setQuery(queryUrl);
                        
                        QNetworkRequest request(url);
                        request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
                        
                        QNetworkReply *reply =  m_auth->networkAccessManager()->post(request,"");
                        connect(reply, &QNetworkReply::finished,this,&Dropbox::getRefreshToken);
                    }
                }
            });
    
    connect(m_auth, &QOAuth2AuthorizationCodeFlow::granted, [=](){
        qDebug() << "token : " << m_auth->token() << " expire at : " << m_auth->expirationAt();
        m_accessToken=m_auth->token();
        m_accessTokenExpireAt=(m_auth->expirationAt().toString("dd MM HH:mm:ss yyyy"));
    });
    m_auth->grant();
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 31 Oct 2023, 21:07 last edited by
      #2

      Hi and welcome to devnet,

      Did you check the content of the query url ?
      Do you have some mismatch like localhost vs 127.0.0.1 ? While they resolve to the same address they are different from an OAuth point of view.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      1/2

      30 Oct 2023, 22:11

      • Login

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