arm*_*ali 5 c++ qt google-authentication oauth-2.0 qt5
问题在于重定向 URI,我不知道将其设置为什么。有谁能够弄清楚这一点吗?
Qt Creator's我在输出窗格中收到如下错误:
qt.networkauth.oauth2: Unexpected call
qt.networkauth.replyhandler: Error transferring https://oauth2.googleapis.com/token - server replied: Bad Request
Run Code Online (Sandbox Code Playgroud)
这是我的代码,一个调用的函数grant()将返回 true open 成功的身份验证。帮助器类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;
}
Run Code Online (Sandbox Code Playgroud)
对我做错了什么有什么想法吗?我设置的重定向 URI http://127.0.0.1:65535/,我猜这就是我做错的事情?
更新:
以下代码正在运行,我遇到问题的原因是因为在获得授权一次后,我再次运行代码,并且由于我已经获得授权,所以我收到了此错误。
最好QOAuth2AuthorizationCodeFlow在堆上创建一个实例,就像 @Chilarai 在他的示例代码中所做的那样。因为无论如何我们都不希望QOAuth2AuthorizationCodeFlow超出范围,因为我们将需要它来提出进一步的请求。
这里的另一个重要注意事项是连接到QOAuthHttpServerReplyHandler::tokensReceived信号,以获得进一步与您的 Google 服务交互所需的令牌。
稍后可以通过 Google REST Api 测试令牌是否仍然有效,这是一种方法,如果您想与之交互,Google Drive可以尝试这个答案的建议。
我很难调试它。但是,我意识到,如果您转到 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();
});
});
Run Code Online (Sandbox Code Playgroud)
有关其余代码的详细信息,请参阅如何使用 Qt oauth 创建登录页面?
| 归档时间: |
|
| 查看次数: |
2308 次 |
| 最近记录: |