c++ - 无法使用 QOAuth2AuthorizationCodeFlow 实施 Google 登录
问题描述
问题在于重定向 URI,我不知道将其设置为什么。有没有人能够解决这个问题?
Qt Creator's
我在输出窗格中收到如下所示的错误:
qt.networkauth.oauth2: Unexpected call
qt.networkauth.replyhandler: Error transferring https://oauth2.googleapis.com/token - server replied: Bad Request
这是我的代码,一个调用的函数grant()
将返回真正的打开成功身份验证。帮助程序类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;
}
关于我做错了什么有什么想法吗?我设置的重定向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();
});
});
有关其余代码的详细信息,请参阅如何使用 Qt oauth 创建登录页面?
推荐阅读
- travis-ci - 使用 Travis CI 发布到 NPMJS
- angular - 使用自定义指令值选择性地显示表单元素
- aws-lambda - 在 AWS Lambda 中增加重试次数
- python - 无法在 Windows 上使用 winsound 播放 .mp3 文件
- powershell - 处理列表时的 Powershell 错误处理
- c# - 从 TextBox 访问值
- c# - C# 整数?参数-数据为空。不能对 Null 值调用此方法或属性
- google-chrome - 浏览器会根据哪些标准考虑使用“application/x-www-form-urlencoded”作为内容类型?
- javascript - Ajax 更新 textarea 没有按钮或页面刷新
- javascript - 根据单词的数量生成计时器倒计时