Log in with access token
-
wrote on 15 Dec 2022, 16:52 last edited by
I try to access DropBox account in my app. I use QOAuth2AuthorizationCodeFlow.
I have no problem to do it with authorizeWithBrowser but a webpage open everytime I launch my app and asked for authorization. I would like to use credentials once and after connect to DropBox using Access Token and Refresh Token.
But I don't understand how it's working !Here my code :
m_auth = new QOAuth2AuthorizationCodeFlow(this); QOAuthHttpServerReplyHandler* replyHandler = new QOAuthHttpServerReplyHandler(portCallback,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(apiKey); m_auth->setClientIdentifierSharedKey(apiSecret); m_auth->setNetworkAccessManager(new QNetworkAccessManager(this)); connect(m_auth, &QAbstractOAuth2::authorizeWithBrowser, [=](QUrl url) { QUrlQuery query(url); query.addQueryItem("force_reapprove", "false"); query.addQueryItem("token_access_type", "offline"); query.addQueryItem("response_type", "code"); url.setQuery(query); QDesktopServices::openUrl(url); }); connect(m_auth, &QAbstractOAuth::granted, [=](){ m_isConnected=true; m_deviceData->setConnected(true); qDebug() << "token : " << m_auth->token() << " expire at : " << m_auth->expirationAt(); //Can I save it here ? Why should I go through autorizationCallbackReceived ? }); connect(m_auth, &QAbstractOAuth2::authorizationCallbackReceived,[=](const QVariantMap data) { if (false == data.isEmpty()) { QString authCode = data.value("code").toString(); if(!authCode.isEmpty()) { // What do I need to do here ? } } }); m_auth->grant();
I suppose I need to check/set my access token instead of asked for grant(); Or should I ask for grant in every case but then how the program know that it didn't need to go through all the authorization process ?
Plus, this is for Access Token but I will need a Refresh Token to reload my Access Token (If I get it right...). So how do I get my refresh token and how do I use it ?
I hope I'm clear about where I'm lost. I read a lot of document/exemple but they never explain the all mechanism...
I thanks for any help.
-
I try to access DropBox account in my app. I use QOAuth2AuthorizationCodeFlow.
I have no problem to do it with authorizeWithBrowser but a webpage open everytime I launch my app and asked for authorization. I would like to use credentials once and after connect to DropBox using Access Token and Refresh Token.
But I don't understand how it's working !Here my code :
m_auth = new QOAuth2AuthorizationCodeFlow(this); QOAuthHttpServerReplyHandler* replyHandler = new QOAuthHttpServerReplyHandler(portCallback,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(apiKey); m_auth->setClientIdentifierSharedKey(apiSecret); m_auth->setNetworkAccessManager(new QNetworkAccessManager(this)); connect(m_auth, &QAbstractOAuth2::authorizeWithBrowser, [=](QUrl url) { QUrlQuery query(url); query.addQueryItem("force_reapprove", "false"); query.addQueryItem("token_access_type", "offline"); query.addQueryItem("response_type", "code"); url.setQuery(query); QDesktopServices::openUrl(url); }); connect(m_auth, &QAbstractOAuth::granted, [=](){ m_isConnected=true; m_deviceData->setConnected(true); qDebug() << "token : " << m_auth->token() << " expire at : " << m_auth->expirationAt(); //Can I save it here ? Why should I go through autorizationCallbackReceived ? }); connect(m_auth, &QAbstractOAuth2::authorizationCallbackReceived,[=](const QVariantMap data) { if (false == data.isEmpty()) { QString authCode = data.value("code").toString(); if(!authCode.isEmpty()) { // What do I need to do here ? } } }); m_auth->grant();
I suppose I need to check/set my access token instead of asked for grant(); Or should I ask for grant in every case but then how the program know that it didn't need to go through all the authorization process ?
Plus, this is for Access Token but I will need a Refresh Token to reload my Access Token (If I get it right...). So how do I get my refresh token and how do I use it ?
I hope I'm clear about where I'm lost. I read a lot of document/exemple but they never explain the all mechanism...
I thanks for any help.
wrote on 15 Dec 2022, 17:38 last edited by Pl45m4Hi,
so you want to login once with your account and then use the token to authenticate until you log out?!
There is this post on StackOverflow
where this libary is recommended. Seems to work for OP
(if you read the manual on GitHub, there is a section, how to store the token persistently for later use)Unfortunately I dont know exacly whether it's possible using
QAuth
only and if so, how.Hope, it helps :)
-
wrote on 19 Dec 2022, 08:49 last edited by
Thanks but I don't want to import any library.
I will check if i understand it better in the code of this library, but for know it's still very confusing.
1/3