Dropbox QOauth module issue
-
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();
-
Hi and welcome to devnet,
Did you check the content of the query url ?
Do you have some mismatch likelocalhost
vs127.0.0.1
? While they resolve to the same address they are different from an OAuth point of view.