Google OAuth 2.0 使用 Qt 安装的应用程序。 "Missing required parameter: code"



我正在编写代码登录到Google+与OAuth 2.0。这是一个使用c++和Qt框架安装的应用程序。为了获得授权码,我使用QWebView显示Google登录页面,并在标题中返回代码。我注意到我以编程方式获得的标题比显示的标题短,并且缺少句点和句点之后的所有字符。接下来,我使用这个授权代码(可能缺少一部分)来获得访问令牌。我在尝试获取访问令牌时收到此错误。您知道这段代码是否有问题导致http错误响应吗?谢谢你。

***** reply http error = 299 
***** response ***** "{
"error" : "invalid_request",
"error_description" : "Missing required parameter: code"
}" 

这是获取访问令牌的代码:

void Application::loginWithAuthCode(QString authCode)
{
    if (authCode.isEmpty())
    {
        qDebug() << "***** Error: Auth code is empty string !!!! *****";
    }
    else
    {
        QUrl serviceUrl = QUrl("https://accounts.google.com/o/oauth2/token");
        QByteArray postData;
        QUrl params;
        QUrlQuery query;
        QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
        QObject::connect(networkManager, SIGNAL(finished(QNetworkReply*)),
             this, SLOT(finishedSlot(QNetworkReply*)));
        qDebug() << "***** auth code ***** = " << authCode;
        query.addQueryItem("code", authCode);
        query.addQueryItem("client_id", "444444444444-mththyjthyjthththyjulrgthrgefrgt.apps.googleusercontent.com");
        query.addQueryItem("client_secret", "222222222-33333333333333");
        query.addQueryItem("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
        query.addQueryItem("grant_type", "authorization_code");
        params.setQuery(query);
        qDebug() << "***** params *****" << params;
        postData = params.toEncoded(QUrl::RemoveFragment);
        qDebug() << "***** postData *****-->" << postData;
        QNetworkRequest request(serviceUrl);
        request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
        qDebug() << "***** request url=" << request.url();
        qDebug() << "***** request content type header=" << request.ContentTypeHeader;
        qDebug() << "***** call POST *****";
        networkManager->post(request, postData);
    }
}
void Application::finishedSlot(QNetworkReply* reply)
{
    qDebug() << "***** finishedSlot! *****";
    // Reading attributes of the reply
    // e.g. the HTTP status code
    QVariant statusCodeV =
    reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    // Or the target URL if it was a redirect:
    QVariant redirectionTargetUrl =
    reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    // see CS001432 on how to handle this
    // no error received?
    if (reply->error() == QNetworkReply::NoError)
    {
        // read data from QNetworkReply here
        // Example 2: Reading bytes form the reply
        QByteArray bytes = reply->readAll();  // bytes
        QString stringResponse(bytes); // string
        qDebug() << "***** response *****" << stringResponse;
    }
    // Some http error received
    else
    {
        qDebug() << "***** reply http error =" << reply->error();
        QByteArray bytes = reply->readAll();  // bytes
        QString stringResponse(bytes); // string
        qDebug() << "***** response *****" << stringResponse;
    }
    // We receive ownership of the reply object
    // and therefore need to handle deletion.
    delete reply;
}

程序输出:

********* titleChanged ********* 
title = "Success code=2/0123456789012345678912345670" 
****************** code = "2/0123456789012345678912345670" 
***** auth code ***** =  "2/0123456789012345678912345670" 
***** params *****  QUrl( "?code=2/0123456789012345678912345670&client_id=444444444444-   mththyjthyjthththyjulrgthrgefrgt.apps.googleusercontent.com&client_secret=222222222-
33333333333333&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" ) 
***** postData *****--> "?code=2/0123456789012345678912345670&client_id=444444444444-  mththyjthyjthththyjulrgthrgefrgt.apps.googleusercontent.com&client_secret=222222222-
33333333333333&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" 
***** request url=  QUrl( "https://accounts.google.com/o/oauth2/token" )  
***** request content type header= 0 
***** call POST ***** 
***** finishedSlot! ***** 
***** reply http error = 299 
***** response *****
"{
"error" : "invalid_request",
"error_description" : "Missing required parameter: code"
}" 

Google文档OAuth 2.0安装应用程序:https://developers.google.com/accounts/docs/OAuth2InstalledApp

较短的授权码可以工作。问题是在http post中的问号。

解决方法:

重定向URL.env文件应该是相同的URL在谷歌开发人员帐户

仔细检查你的重定向url。

我们在使用Google SSO验证qt应用程序时发现的一些常见问题:

  • Qt建议您使用的URL不会导致我们的应用程序识别事件。相反,使用http://127.0.0.1:1234/(/很重要,并且使用IP而不是localhost以避免某些客户端出现问题)
  • 从Google收到的登录码需要进行url解码

相关内容

  • 没有找到相关文章

最新更新