【发布时间】:2014-09-26 09:46:00
【问题描述】:
我正在编写代码以使用 OAuth 2.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;
}
程序输出:
********* 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"
}"
关于已安装应用程序的 OAuth 2.0 的 Google 文档: https://developers.google.com/accounts/docs/OAuth2InstalledApp
【问题讨论】:
-
Qt 是一个框架,而不是一种语言。
-
你的电话在我看来不合适。试试看这个。 daimto.com/google-3-legged-oauth2-flow
标签: qt oauth-2.0 google-api google-plus google-api-client