gpt4 book ai didi

c++ - 谷歌oauth2.0,必需的参数丢失: grant_type

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:48:07 27 4
gpt4 key购买 nike

我按照谷歌文档为我公司的桌面应用做了一个“用谷歌登录”,文档链接: https://developers.google.com/identity/protocols/OAuth2InstalledApp

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

“错误”:“无效请求”

“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 必须是 real URI如果“redirect_uri”参数包含在第 4.1.1 节(RFC 6749)中所述的授权请求中,则必须匹配,并且它们的值必须相同。 uri 必须类似于 scheme:[//[user:password@]host[:port]][/]path

关于c++ - 谷歌oauth2.0,必需的参数丢失: grant_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34388889/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com