QAbstractOAuth2 missing access_type and approval_prompt?
-
The (horribly convoluted) code I (a novice) have inherited uses the QAbstractOAuth2 class to implement OAuth2 access to Google drive.
The app only succeeds to gain a access token for Google Drive if the app's access is revoked from the Google Drive control panel. But subsequent requests fail with the obscure 'bad request' error.
I set up a test using curl and found that adding the parameters
access_type=offline&approval_prompt=force
to the URL results in the access working.So now I want to try and add this in my Qt code.
_oAuth2 = new QOAuth2AuthorizationCodeFlow( this); _oAuth2->setScope( "https://www.googleapis.com/auth/drive"); _oAuth2->setAuthorizationUrl( "https://accounts.GOOGLE.com/o/oauth2/auth" ); _oAuth2->setAccessTokenUrl( "https://oauth2.googleapis.com/token"); _oAuth2->setClientIdentifier( "the_client_id"); _oAuth2->setClientIdentifierSharedKey( "the secret_key"); _firstAuth = true; _oAuth2->grant();
The resulting URL is:
https://accounts.google.com/o/oauth2/auth?client_id=the_client_id&redirect_uri=http://127.0.0.1:8080/cb&response_type=code&scope=https://www.googleapis.com/auth/drive&state=2hRrr5q1
There I can add the access_type and approval_prompt to the URL but shouldn't these be included in the QAbstractOAuth2 class ?
-
I found the answer. From this article :
How To Authenticate with Google SSO in Qt (C++)
I found that the reply must be assumed to return encoded code, so adding call to
setModifyParametersFunction
as below fixed the issue:_oAuth2 = new QOAuth2AuthorizationCodeFlow( this); _oAuth2->setScope( "https://www.googleapis.com/auth/drive"); _oAuth2->setAuthorizationUrl( "https://accounts.GOOGLE.com/o/oauth2/auth" ); _oAuth2->setAccessTokenUrl( "https://oauth2.googleapis.com/token"); _oAuth2->setClientIdentifier( "the_client_id"); _oAuth2->setClientIdentifierSharedKey( "the secret_key"); // Added this: _oAuth2->setModifyParametersFunction([](QAbstractOAuth::Stage stage, QVariantMap* parameters) { // Percent-decode the "code" parameter so Google can match it if (stage == QAbstractOAuth::Stage::RequestingAccessToken) { QByteArray code = parameters->value("code").toByteArray(); (*parameters)["code"] = QUrl::fromPercentEncoding(code); } }); // End Add _firstAuth = true; _oAuth2->grant();```
-
@TenG for QT 6.1.0 this lambda setModifyParametersFunction([](QAbstractOAuth::Stage stage, QVariantMap* parameters) isnt work.
I got error :
no viable conversion from lambda to 'const QAbstractOAuth::ModifyParametersFunction' (aka 'const function<void (QAbstractOAuth::Stage, QMultiMap<QString, QVariant> *)>') -
same thing
-
Hi how did you fix it? I have the same problem
-
@franco-amato hi,
Which version of Qt are you using ?
The issue of percent encoded value handling has been fixed since then.
-
@SGaist I am using Qt 6.7.2, I tried everything and nothing
m_googleOAuth2->setModifyParametersFunction( [this](QAbstractOAuth::Stage stage, QVariantMap* parameters) { modifyOAuthParameters(stage, parameters); });
void Login::modifyOAuthParameters(QAbstractOAuth::Stage stage, QVariantMap *parameters)
{
if (stage == QAbstractOAuth::Stage::RequestingAuthorization)
{
parameters->insert("redirect_uri", "http://localhost:8080/");
}
}