无法使用QOAuth2AuthorizationCodeFlow实施Google登录

问题描述 投票:1回答:1

问题在于重定向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/,我猜这是我做错了吗?

c++ qt oauth-2.0 qt5 google-authentication
1个回答
0
投票

我很难调试它。但是,我已经意识到,如果您转到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(MY_CLIENT_ID);
        this->google->setAccessTokenUrl(QUrl("https://oauth2.googleapis.com/token"));
        this->google->setClientIdentifierSharedKey(MYTOKEN);

// In my case, I have hardcoded 8080 to test
        auto replyHandler = new QOAuthHttpServerReplyHandler(8080, this);
        this->google->setReplyHandler(replyHandler);
        this->google->grant();

        qDebug() << "Access";

        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();
            });
        });

有关其余代码的详细信息,请参阅此How to create a login page using Qt oauth?

© www.soinside.com 2019 - 2024. All rights reserved.