GoogleOAuth2.0使用Qt安装了应用程序&引用;缺少必需参数:代码";

GoogleOAuth2.0使用Qt安装了应用程序&引用;缺少必需参数:代码";,qt,oauth-2.0,google-api,google-plus,google-api-client,Qt,Oauth 2.0,Google Api,Google Plus,Google Api Client,我正在编写使用OAuth2.0登录Google+的代码。这是一个安装了C++和QT框架的应用程序。为了获得授权代码,我使用QWebView显示Google登录页面,代码在标题中返回。我注意到以编程方式获得的标题比显示的标题短,并且缺少句点和句点后的所有字符。接下来,我使用这个授权代码(可能缺少部分)来获取访问令牌。我在尝试获取访问令牌时收到此错误。您知道这段代码是否有错误导致http错误响应吗?多谢各位 ***** reply http error = 299 ***** response *

我正在编写使用OAuth2.0登录Google+的代码。这是一个安装了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;
}
OAuth 2.0上已安装应用程序的Google文档:

较短的授权码将起作用。问题在于http post中的问号。http post不应是查询。

由以下人员解决:

.env
文件中的重定向URL应与google开发者帐户中的URL相同

仔细检查您的重定向URL


我们在处理时发现的一些常见问题:

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

Qt是一个框架,而不是一种语言。我觉得你的调用不太合适。试试看这个。
********* 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"
}"