【问题标题】:Google oauth2.0, required parameter is missing: grant_typeGoogle oauth2.0,缺少必需参数:grant_type
【发布时间】:2015-12-22 08:54:34
【问题描述】:

我按照 Google 文档为我的公司桌面应用程序制作了“使用 Google 登录”,文档链接: https://developers.google.com/identity/protocols/OAuth2InstalledApp

问题是当我发送一个 post 请求来检索 access_token 时,我总是收到这个错误:

“错误”:“无效请求”

“error_description”:“缺少必需参数:grant_type”

我确实在 Google 上搜索了几天,但不知道发生了什么。如果这是一个愚蠢的问题,请原谅我,我对http一无所知。

这是我的代码。在我的代码中,我使用此链接中的库: http://www.codeproject.com/Articles/66625/A-Fully-Featured-Windows-HTTP-Wrapper-in-C

#include "RegExp.h"
#include "StringProcess.h"
#include "WinHttpClient.h"

#include <iostream>

using namespace std;

void main(void)
{
    WinHttpClient client(L"https://accounts.google.com/o/oauth2/token");

    //data of post request
    string data = "";

    data += "code=";
    string code = "some_code";
    data += code;

    data += "&client_id=";
    string client_id = "my_app_id";
    data += client_id;

    data += "&client_secret=";
    string client_secret = "my_app_secret";
    data += client_secret;

    data += "&redirect_uri";
    string redirect_uri = "urn:ietf:wg:oauth:2.0:oob";
    data += redirect_uri;

    data += "&grant_type=";
    string grant_type = "authorization_code";
    data += grant_type;


    client.SetAdditionalDataToSend((BYTE *)data.c_str(), data.size());

    //header of post request
    wstring headers = L"Content-Length: ";
    headers += L"\r\nContent-Type: application/x-www-form-urlencoded\r\n";
    wchar_t szHeaders[MAX_PATH * 10] = L"";
    swprintf_s(szHeaders, MAX_PATH * 10, headers.c_str(), data.size());
    client.SetAdditionalRequestHeaders(szHeaders);

    //send request and print response
    client.SendHttpRequest(L"POST");

    wstring httpResponseHeader = client.GetResponseHeader();
    wstring httpResponseContent = client.GetResponseContent();

    char content[10000];
    sprintf(content, "%ls", httpResponseContent.c_str() );

    char header[10000];
    sprintf(header, "%ls", httpResponseHeader.c_str());

    cout << header  << endl;
    cout << content << endl;

    system("pause");
}

非常感谢

【问题讨论】:

  • redirect_uri 后面不应该有=,redirect_uri 不应该是URL 编码的吗?
  • @immibis 谢谢 1/ 我添加了一个“=”,仍然没有变化 2/ 你的意思是:“headers += L”\r\nContent-Type: application/x-www-form -urlencoded\r\n"; ?
  • 不,immibis 表示需要在 redirect_uri 的值中对冒号进行 URL 编码(=urn%3Aietf%3A 等)。
  • @1201ProgramAlarm:我尝试修改uri但仍然没有变化

标签: c++ oauth-2.0


【解决方案1】:

您的redirect_uri 必须是real URI,并且必须与第4.1.1 节中所述的授权请求中包含“redirect_uri”参数(RFC 6749)相匹配,并且它们的值必须相同。 uri 必须类似于 scheme:[//[user:password@]host[:port]][/]path

【讨论】:

    猜你喜欢
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 2012-06-27
    • 2019-12-11
    • 2012-01-02
    • 1970-01-01
    • 2019-09-14
    相关资源
    最近更新 更多