gpt4 book ai didi

c# - 请求访问 token 时通过 OAuth 2、 "invalid_request"连接到 Google

转载 作者:太空宇宙 更新时间:2023-11-03 19:17:52 24 4
gpt4 key购买 nike

SE 上已经有几个类似的问题,但我已经阅读了所有我能找到的似乎相关的内容,但我仍然没有完全理解。

我得到了一个验证码,所以现在我需要用它来交换访问 token 和刷新 token 。然而,Google 返回了非常不明确的错误“invalid_request”。这是我的代码:

private const string  BaseAccessTokenUrl = "https://accounts.google.com/o/oauth2/token";
private const string ContentType = "application/x-www-form-urlencoded";

public static string GetRefreshToken(string clientId, string clientSecret, string authCode)
{
Dictionary<string, string> parameters = new Dictionary<string, string>
{
{ "code", authCode },
{ "client_id", clientId },
{ "client_secret", clientSecret },
{ "redirect_uri", "http://localhost" },
{ "grant_type", "authorization_code" }
};
string rawJson = WebUtilities.Post(BaseAccessTokenUrl, parameters, ContentType);
return rawJson; // TODO: Parse out the actual refresh token
}

我的 Post() 方法对参数键和值进行 URL 编码并将它们连接起来:

public static string  Post(string uri, Dictionary<string, string> properties, string contentType = "application/x-www-form-urlencoded")
{
string content = String.Join("&", from kvp in properties select UrlEncode(kvp.Key) + "=" + UrlEncode(kvp.Value) );
return Post(uri, content);
}

双参数 Post() 方法只处理将内容转换为字节、添加内容长度等,然后返回响应的内容,即使它来自 WebException。如果有任何兴趣,我可以将其包括在内。

授权码看起来是对的,和我见过的其他类似:62个字符,以“4/”开头。我从 the Google API Console 中仔细复制的客户端 ID、密码和重定向 URL .该应用已注册为“其他”应用,我正在从 Windows 计算机进行连接。

根据 thisthis post ,我试过不进行 URL 编码,没有任何变化。 The OAuth Playground表明 URL 编码是正确的。

根据 this postthis one , 属性连接在一行上。

根据 this post ,我已经在授权请求中尝试了 approval_prompt=force,但是新的授权代码并没有更好地工作。授权码会过期吗?通常,我会在几秒钟内使用新代码。

根据 the Google docsthis post ,我正在使用内容类型“application/x-www-form-encoded”。

我的授权请求是针对范围“https://www.googleapis.com/auth/analytics.readonly”。

根据 this post , 参数中没有前导问号。

有一个 Google .NET OAuth 库,但我无法轻松使用它,如果有选择的话,大约 50,000 行代码超出了我想研究的范围.我更喜欢从头开始写一些干净的东西,而不是盲目地复制一堆库, cargo 崇拜风格。

最佳答案

找到了。用于请求 token 的 redirect_uri 需要与获取授权代码时使用的相匹配。这是我获取授权码的工作代码:

private const string  BaseAuthorizationUrl = "https://accounts.google.com/o/oauth2/auth";
public string GetAuthorizationUrl(string clientId, IEnumerable<string> scopes)
{
var parameters = new Dictionary<string, string>
{
{ "response_type", "code" },
{ "client_id", clientId },
{ "redirect_uri", RedirectUrl },
{ "scope", String.Join(" ", scopes) },
{ "approval_prompt", "auto" }
};
return WebUtilities.BuildUrl(BaseAuthorizationUrl, parameters);
}

...这是我获取访问 token 和刷新 token 的代码:

private const string  BaseAccessTokenUrl = "https://accounts.google.com/o/oauth2/token";
public void GetTokens(string clientId, string clientSecret, string authorizationCode, out string accessToken, out string refreshToken)
{
var parameters = new Dictionary<string, string>
{
{ "code", authorizationCode },
{ "redirect_uri", RedirectUrl }, // Must match that used when authorizing an app
{ "client_id", clientId },
{ "scope", String.Empty },
{ "client_secret", clientSecret },
{ "grant_type", "authorization_code" }
};
string rawJson = WebUtilities.Post(BaseAccessTokenUrl, parameters, "application/x-www-form-urlencoded");
dynamic parsedJson = JsonUtilities.DeserializeObject(rawJson);
accessToken = parsedJson.access_token;
refreshToken = parsedJson.refresh_token;
}

...这是获取新访问 token 的代码:

public string  GetAccessToken(string clientId, string clientSecret, string refreshToken)
{
var parameters = new Dictionary<string, string>
{
{ "client_id", clientId },
{ "client_secret", clientSecret },
{ "refresh_token", refreshToken },
{ "grant_type", "refresh_token" }
};
string rawJson = WebUtilities.Post(BaseAccessTokenUrl, parameters, "application/x-www-form-urlencoded");
dynamic parsedJson = JsonUtilities.DeserializeObject(rawJson);
return parsedJson.access_token;
}

关于c# - 请求访问 token 时通过 OAuth 2、 "invalid_request"连接到 Google,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14883913/

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