将Google API接入Qt应用时遭遇OAuth2认证异常问题
Qt OAuth2 Google登录失败:未收到认证信息,报"Unexpected call"错误
我参考Qt官方博客实现了Qt应用中的Google登录功能,编写了GoogleGateway类处理OAuth2认证流程。目前点击GUI按钮可以正常跳转至浏览器完成登录操作,但调试时出现以下错误,应用始终无法收到认证信息:
qt.networkauth.oauth2: Unexpected call qt.networkauth.replyhandler: Error transferring https://oauth2.googleapis.com/token - server replied:
相关代码
#include "googleapi.h" #include <QObject> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QString> #include <QFile> #include <QDir> #include <QUrl> #include <QOAuthHttpServerReplyHandler> #include <QDesktopServices> GoogleGateway::GoogleGateway(QObject* parent) : QObject(parent) { this->google = new QOAuth2AuthorizationCodeFlow(this); this->google->setScope("email"); connect(this->google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl); QByteArray val; QFile file; file.setFileName(QDir::toNativeSeparators("/path/credential_file.json")); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { val = file.readAll(); file.close(); } QJsonDocument document = QJsonDocument::fromJson(val); QJsonObject object = document.object(); const auto settingsObject = object["installed"].toObject(); //qDebug() << settingsObject["auth_uri"].toString() << settingsObject["client_id"].toString() << settingsObject["token_uri"].toString() << settingsObject["client_secret"].toString(); const QUrl authUri(settingsObject["auth_uri"].toString()); const auto clientId = settingsObject["client_id"].toString(); const QUrl tokenUri(settingsObject["token_uri"].toString()); const auto clientSecret(settingsObject["client_secret"].toString()); const auto redirectUris = settingsObject["redirect_uris"].toArray(); const QUrl redirectUri(redirectUris[0].toString()); const auto port = static_cast<quint16>(redirectUri.port()); this->google->setAuthorizationUrl(authUri); this->google->setClientIdentifier(clientId); this->google->setAccessTokenUrl(tokenUri); this->google->setClientIdentifierSharedKey(clientSecret); auto replyHandler = new QOAuthHttpServerReplyHandler(8080, 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(); }); }); }
已排查内容
- 确认网络连接正常
- 检查重定向URI与Google Cloud控制台配置一致
- 验证客户端ID、密钥等凭证信息正确
- 尝试连接
QOAuth2AuthorizationCodeFlow::error信号,但未获取到有效错误信息
请求技术帮助解决该认证失败问题。
内容的提问来源于stack exchange,提问作者Karim Kamel
相关产品推荐
相关产品推荐

