无法使用QOAuth2AuthorizationCodeFlow实施Google登录 [英] Unable to implement Google Sign-In using QOAuth2AuthorizationCodeFlow

查看:164
本文介绍了无法使用QOAuth2AuthorizationCodeFlow实施Google登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于重定向URI,我不知道将其设置为什么.长谷市任何人都能弄清楚这一点吗?

The issue is with redirect URIs, I don't know what to set it to. Hase ANYONE been able to figure this out?

Qt Creator's输出窗格中出现如下错误:

I get an error in Qt Creator's output pane that looks like this:

qt.networkauth.oauth2: Unexpected call
qt.networkauth.replyhandler: Error transferring https://oauth2.googleapis.com/token - server replied: Bad Request

这是我的代码,一个名为grant()的函数,它将返回真正的打开成功身份验证.助手类OAuth2Props返回Google生成的JSON文件中的所有数据.

Here's my code, a function called grant() that will return true open successful authentication. The helper class OAuth2Props returns all the data from the JSON file generated by Google.

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/,我猜这是我做错了吗?

Any thoughts on what I am doing wrong? The redirect URI I have set to http://127.0.0.1:65535/, I am guessing that's what I am doing wrong?

更新:

  1. 以下代码有效,我遇到麻烦的原因是因为获得一次授权后,我又在运行该代码,并且由于已经获得授权,所以我遇到了此错误.

  1. The following code is working, the reason I was having trouble was because after getting authorized once, I was running the code again and since I was already authorized I was getting this error.

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

It's probably better to create an instance of QOAuth2AuthorizationCodeFlow on the heap, like @Chilarai is doing in his sample code. Because we don't want our QOAuth2AuthorizationCodeFlow to go out of scope anyways, since we are going to be needing it to make further requests.

这里的另一个重要说明是连接到QOAuthHttpServerReplyHandler::tokensReceived信号,以便获得进一步与您的Google服务进行交互所需的令牌.

Another important note here is to connect to QOAuthHttpServerReplyHandler::tokensReceived signal, in order to get the token needed to further interact with your Google service.

稍后可以通过Google REST Api测试令牌是否仍然有效,这是一种方法,如果您想与Google Drive进行交互,则可以尝试以下

The token can later be tested if it is still valid through a Google REST Api, here's one way to do it, if you want to interact with Google Drive you can try what this answer suggests.

推荐答案

我很难调试它.但是,我已经意识到,如果您转到Google控制台并将重定向URI设置为http://127.0.0.1:some_port/而不是http://localhost:some_port/

I had a hard time debugging it. But, I have realized that if you goto Google console and set the Redirect URI to http://127.0.0.1:some_port/ instead of http://localhost:some_port/

记住要在末尾加上'/'

Remember to put '/' in the end

它神奇地工作.剩下的就是我的代码

it magically works. Rest here is my code

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

有关其余代码的详细信息,请参阅此

For details on the rest of the code, refer this How to create a login page using Qt oauth?

这篇关于无法使用QOAuth2AuthorizationCodeFlow实施Google登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆