问题在于重定向 URI,我不知道将其设置为什么。有谁能够弄清楚这一点吗?
我收到错误Qt Creator's
输出窗格如下所示:
qt.networkauth.oauth2: Unexpected call
qt.networkauth.replyhandler: Error transferring https://oauth2.googleapis.com/token - server replied: Bad Request
这是我的代码,一个名为grant()
这将返回 true 打开成功的身份验证。辅助类OAuth2Props
返回 Google 生成的 JSON 文件中的所有数据。
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;
}
对我做错了什么有什么想法吗?我设置的重定向 URIhttp://127.0.0.1:65535/
,我猜我做错了什么?
Update:
-
以下代码正在运行,我遇到问题的原因是因为在获得授权一次后,我再次运行代码,并且由于我已经获得授权,所以我收到了此错误。
-
创建一个实例可能会更好QOAuth2AuthorizationCodeFlow
在堆上,就像 @Chilarai 在他的示例代码中所做的那样。因为我们不想要我们的QOAuth2AuthorizationCodeFlow
无论如何都会超出范围,因为我们将需要它来提出进一步的请求。
-
这里另一个重要的注意事项是连接到QOAuthHttpServerReplyHandler::tokensReceived
信号,以便获取与您的 Google 服务进一步交互所需的令牌。
-
稍后可以通过 Google REST Api 测试令牌是否仍然有效,如果您想与Google Drive
你可以试试这个answer https://stackoverflow.com/questions/52181208/why-am-i-getting-server-replied-forbidden-from-google-drive-rest-api建议。
我很难调试它。但是,我意识到,如果您转到 Google 控制台并将重定向 URI 设置为http://127.0.0.1:some_port/
代替http://localhost:some_port/
记得最后加“/”
它神奇地起作用了。休息这里是我的代码
this->google = new QOAuth2AuthorizationCodeFlow(this);
this->google->setScope("email");
connect(this->google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl);
this->google->setAuthorizationUrl(QUrl("https://accounts.google.com/o/oauth2/auth"));
this->google->setClientIdentifier(CLIENT_ID);
this->google->setAccessTokenUrl(QUrl("https://oauth2.googleapis.com/token"));
this->google->setClientIdentifierSharedKey(CLIENT_SECRET);
// In my case, I have hardcoded 5476 to test
auto replyHandler = new QOAuthHttpServerReplyHandler(5476, this);
this->google->setReplyHandler(replyHandler);
this->google->grant();
connect(this->google, &QOAuth2AuthorizationCodeFlow::granted, [=](){
qDebug() << __FUNCTION__ << __LINE__ << "Access Granted!";
auto reply = this->google->get(QUrl("https://www.googleapis.com/plus/v1/people/me"));
connect(reply, &QNetworkReply::finished, [reply](){
qDebug() << "REQUEST FINISHED. Error? " << (reply->error() != QNetworkReply::NoError);
qDebug() << reply->readAll();
});
});
有关其余代码的详细信息,请参阅此如何使用 Qt oauth 创建登录页面? https://stackoverflow.com/questions/48453550/how-to-create-a-login-page-using-qt-oauth/62382316#62382316
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)