Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法使用QOAuth2AuthorizationCodeFlow实现Google登录_C++_Qt_Oauth 2.0_Qt5_Google Authentication - Fatal编程技术网

C++ 无法使用QOAuth2AuthorizationCodeFlow实现Google登录

C++ 无法使用QOAuth2AuthorizationCodeFlow实现Google登录,c++,qt,oauth-2.0,qt5,google-authentication,C++,Qt,Oauth 2.0,Qt5,Google Authentication,问题是重定向URI,我不知道该设置什么。有人能弄明白吗 我在Qt Creator的输出窗格中发现一个错误,如下所示: qt.networkauth.oauth2: Unexpected call qt.networkauth.replyhandler: Error transferring https://oauth2.googleapis.com/token - server replied: Bad Request 这是我的代码,一个名为grant()的函数,它将返回真正的开放式成功身份验

问题是重定向URI,我不知道该设置什么。有人能弄明白吗

我在
Qt Creator的
输出窗格中发现一个错误,如下所示:

qt.networkauth.oauth2: Unexpected call
qt.networkauth.replyhandler: Error transferring https://oauth2.googleapis.com/token - server replied: Bad Request
这是我的代码,一个名为
grant()
的函数,它将返回真正的开放式成功身份验证。helper类
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;
}
有没有想过我做错了什么?我已设置为
http://127.0.0.1:65535/
,我猜这就是我做错的地方吧

更新:

  • 下面的代码正在运行,我遇到问题的原因是,在获得一次授权后,我再次运行代码,并且由于我已经获得授权,所以出现了此错误

  • 最好在堆上创建
    QOAuth2AuthorizationCodeFlow
    的实例,就像@Chilarai在他的示例代码中所做的那样。因为我们不希望我们的
    QOAuth2AuthorizationCodeFlow
    超出范围,因为我们需要它来发出进一步的请求

  • 这里的另一个重要注意事项是连接到
    QOAuthHttpServerReplyHandler::tokensReceived
    signal,以便获得进一步与Google服务交互所需的令牌

  • 如果令牌通过Google REST Api仍然有效,那么可以稍后对其进行测试,这里有一种方法,如果您想与
    Google Drive
    交互,可以尝试这一方法


  • 我很难调试它。但是,我已经意识到,如果你转到谷歌控制台并将重定向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();
                });
            });
    
    this->google=newqoauth2authorizationcodeflow(this);
    这->谷歌->设置范围(“电子邮件”);
    连接(this->google,&QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,&QDesktopServices::openUrl);
    此->谷歌->设置授权URL(QUrl(“https://accounts.google.com/o/oauth2/auth"));
    此->谷歌->设置客户端标识(客户端标识);
    这->谷歌->设置访问令牌URL(QUrl(“https://oauth2.googleapis.com/token"));
    此->谷歌->设置客户端标识符SharedKey(客户端密码);
    //在我的例子中,我已经硬编码5476进行测试
    auto replyHandler=新的QOAuthHttpServerReplyHandler(5476,本);
    这->谷歌->设置replyHandler(replyHandler);
    这->谷歌->格兰特();
    连接(此->谷歌,&QOAuth2AuthorizationCodeFlow::已授予,[=](){
    
    qDebug()鉴于Google阻止来自嵌入式web视图的OAuth请求,我们发布了一篇关于官方解决方案中常见问题的解释,并提供了一些入门指南

    我们发现的更常见的问题是:

    • Qt示例中的解决方案使您使用了错误的重定向URL
    • Google返回的登录代码需要在身份验证流中进行UR>-解码
    • JS与C++之间的相互作用发生了变化:
    Hi@armanali,看起来您在使用QtNetwork而不是google API时遇到了一些问题。您正在使用哪些操作系统进行测试?Linux、Windows?您是否在正确的位置安装了OpenSSL和加密库?要从networkauth获得更多调试,请执行以下操作:#在主()的第一行包含并设置QLoggingCategory::setFilterRules(“qt.networkauth.*=true”) function@Xplatforms,我现在正在用Windows测试它。我确实从Qt安装程序安装了OpenSSL,并将它添加到我的路径
    C:\Qt\Tools\OpenSSL\Win\u x64\bin
    。我会尝试你的建议。非常感谢!在我的.pro文件中,我添加了
    Qt+=core gui networkauth
    ,我按照你的建议做了,但没有得到任何帮助gful debug Messages刚刚测试了我的旧代码,它也失败了。但是如果你连接到QOAuthHttpServerReplyHandler::callbackReceived slot,你可以看到authufer代码,等等。试着用setNetworkAccessManager设置自己的QNAM子类来调试oAuth流。或者像我那样切换到amazon oauth2:这是我拥有的,但它仍然不起作用。我将使用您的解决方案作为参考再次尝试。另外,您可以展示/发布您如何配置您的Google Auth 2.0凭据吗?您可以只发布JSON的密钥而不发布值。谢谢!这是我的JSON密钥。请记住,我已经在我的代码
    {“web”:{“client\u id”:“client\u id”,“project\u id”:“TEST”中硬编码了上面的值,“auth_uri”:https://accounts.google.com/o/oauth2/auth“,“令牌uri”:”https://oauth2.googleapis.com/token,“身份验证提供程序\u x509\u证书\u url”:https://www.googleapis.com/oauth2/v1/certs,“客户机机密”:“客户机机密”,“重定向URI”:[“http://127.0.0.1:5476/“],“javascript_起源”:[”http://localhost", "http://localhost:5476"]}}
    我已经在Github上实现了一个包含所有详细信息的示例项目。您可能想在这里查看该项目。顺便说一句,我尝试了您的代码,它也工作得很好。它一定是不正确的重定向uri或某个创建问题的端口。我在Qt Creator
    qrc:/main中遇到以下错误。qml:1:module“QtQuick”“2.15版未安装
    。我使用的是Qt 5.14.2,我检查了Qt安装工具,没有单独添加Qt Quick的选项,我猜它是5.14.2版核心的一部分。你有什么版本?一个食蚁兽同伴发来的好帖子。谢谢!:)Zot,Zot!谢谢@aalimian:)