【发布时间】:2015-02-11 14:25:48
【问题描述】:
我正在尝试从 Google 的授权端点检索访问令牌,但我不断收到 401: Unauthorized。
我很确定发送的所有值(授权码、客户端 ID、客户端密码、重定向 uri 和授权类型)都是正确的。
我的代码如下:
using (HttpClient client = new HttpClient()) {
IEnumerable<KeyValuePair<string,string>> data = new List<KeyValuePair<string,string>>
{
new KeyValuePair<string,string>("code", "CODE_HERE"),
new KeyValuePair<string,string>("client_id", "CLIENT_ID_HERE"),
new KeyValuePair<string,string>("client_secret", "CLIENT_SECRET_HERE"),
new KeyValuePair<string,string>("redirect_uri", "REDIRECT_URI_HERE"),
new KeyValuePair<string,string>("grant_type", "authorization_code"),
}
HttpContent content = new FormUrlEncodedContent(data);
/* I'm getting 401 Unauthorized */
HttpResponseMessage response = await client.PostAsync("https://www.googleapis.com/oauth2/v3/token", content);
}
响应的 JSON 是:
{
"error": "invalid_client",
"error_description": "Unauthorized"
}
但是,我正在从我的 Google 开发者控制面板中复制并粘贴客户的 ID 和客户的密码,所以它们不可能是错误的。
有什么帮助吗?
【问题讨论】:
-
当您将凭据传递到 OAuth2.0 Playground 时,您是否仍然收到错误消息? developers.google.com/oauthplayground
-
你在这方面有什么进展吗?
-
@vtortola 非常抱歉。在我设法找到解决方案之前,这个项目就停止了。如果你们碰巧找到了,把它贴在这里,因为我仍然非常有兴趣学习!
标签: c# asp.net oauth oauth-2.0 google-oauth