Google oauth2.0,缺少必需的参数:grant_type

Google oauth2.0, required parameter is missing: grant_type

本文关键字:参数 grant type oauth2 Google      更新时间:2023-10-16

我遵循谷歌文档为我的公司桌面应用程序制作"登录谷歌",文档链接:https://developers.google.com/identity/protocols/OAuth2InstalledApp

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

"error":"invalid_request"

"error_description":"缺少必需参数:grant_type"

我确实在谷歌上搜索了几天,但不知道发生了什么。如果这是一个愚蠢的问题,请原谅,我对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"rnContent-Type: application/x-www-form-urlencodedrn";
    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必须是一个真实的uri,并且必须匹配if;redirect_uri";参数包含在第4.1.1节(RFC 6749)中描述的授权请求中,并且它们的值必须相同。uri必须类似scheme:[//[user:password@]host[:port]][/]path