首页 > 解决方案 > 无法使用 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/,我猜这就是我做错了什么?

更新:

  1. 以下代码正在运行,我遇到问题的原因是因为在获得一次授权后,我再次运行代码,并且由于我已经获得了授权,所以我收到了这个错误。

  2. 在堆上创建一个实例可能会更好QOAuth2AuthorizationCodeFlow,就像@Chilarai 在他的示例代码中所做的那样。因为无论如何我们都不希望我们QOAuth2AuthorizationCodeFlow超出范围,因为我们将需要它来提出进一步的请求。

  3. 这里的另一个重要注意事项是连接到QOAuthHttpServerReplyHandler::tokensReceived信号,以获得进一步与您的 Google 服务交互所需的令牌。

  4. 稍后可以通过 Google REST Api 测试令牌是否仍然有效,这是一种方法,如果您想与之交互,Google Drive可以尝试这个答案的建议。

标签: c++qtoauth-2.0qt5google-authentication

解决方案


我很难调试它。但是,我已经意识到,如果您转到 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 创建登录页面?


推荐阅读