The issue is with redirect URIs, I don't know what to set it to. Hase ANYONE been able to figure this out?
I get an error in Qt Creator's output pane that looks like this:
qt.networkauth.oauth2: Unexpected call
qt.networkauth.replyhandler: Error transferring https://oauth2.googleapis.com/token - server replied: Bad Request
Here's my code, a function called grant() that will return true open successful authentication. The helper class OAuth2Props returns all the data from the JSON file generated by Google.
bool grant() {
QOAuth2AuthorizationCodeFlow oauthFlow;
QObject::connect(&oauthFlow,
&QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
&QDesktopServices::openUrl);
oauthFlow.setScope("email");
oauthFlow.setAuthorizationUrl(OAuth2Props::authUri());
oauthFlow.setClientIdentifier(OAuth2Props::clientId());
oauthFlow.setAccessTokenUrl(OAuth2Props::tokenUri());
oauthFlow.setClientIdentifierSharedKey(OAuth2Props::clientSecret());
QOAuthHttpServerReplyHandler oauthReplyHandler(
QUrl(OAuth2Props::redirectUri()).port());
oauthFlow.setReplyHandler(&oauthReplyHandler);
QEventLoop eventLoop;
QObject::connect(&oauthFlow, &QOAuth2AuthorizationCodeFlow::granted,
&eventLoop, &QEventLoop::quit);
oauthFlow.grant();
eventLoop.exec();
return true;
}
Any thoughts on what I am doing wrong? The redirect URI I have set to http://127.0.0.1:65535/, I am guessing that's what I am doing wrong?
Update:
The following code is working, the reason I was having trouble was because after getting authorized once, I was running the code again and since I was already authorized I was getting this error.
It's probably better to create an instance of
QOAuth2AuthorizationCodeFlowon the heap, like @Chilarai is doing in his sample code. Because we don't want ourQOAuth2AuthorizationCodeFlowto go out of scope anyways, since we are going to be needing it to make further requests.Another important note here is to connect to
QOAuthHttpServerReplyHandler::tokensReceivedsignal, in order to get the token needed to further interact with your Google service.The token can later be tested if it is still valid through a Google REST Api, here's one way to do it, if you want to interact with
Google Driveyou can try what this answer suggests.