【问题标题】:Unable to get access token from Google OAuth2 Token endpoint无法从 Google OAuth2 令牌端点获取访问令牌
【发布时间】: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 非常抱歉。在我设法找到解决方案之前,这个项目就停止了。如果你们碰巧找到了,把它贴在这里,因为我仍然非常有兴趣学习!
  • 希望对您有所帮助:stackoverflow.com/questions/28548920/…

标签: c# asp.net oauth oauth-2.0 google-oauth


【解决方案1】:

这对我有用。在此示例中,我使用 RefreshToken 来获取 AccessToken。

var client_id = ConfigurationManager.AppSettings.Get("GoogleClientId");
                var client_secret = ConfigurationManager.AppSettings.Get("GoogleSecret");
                var grant_type = "refresh_token";

                var url = "https://www.googleapis.com/oauth2/v4/token";

                IEnumerable<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string,string>("client_id", client_id),
                    new KeyValuePair<string,string>("client_secret", client_secret),
                    new KeyValuePair<string,string>("grant_type", grant_type),
                    new KeyValuePair<string,string>("refresh_token", refreshToken),
                };

                var client = new HttpClient();
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

                HttpContent contentPost = new FormUrlEncodedContent(data);

                HttpResponseMessage response = await client.PostAsync(url, contentPost);
                var result = response.Content.ReadAsStringAsync().Result;
                return JsonConvert.DeserializeObject<GoogleAPIAuth>(result);

这是我的 GoogleAPIAuth 类。

public class GoogleAPIAuth
    {
        public string client_secret { get; set; }
        public string grant_type { get; set; }
        public string refresh_token { get; set; }
        public string client_id { get; set; }
        public string access_token { get; set; }
        public string expires_in { get; set; }
        public string token_type { get; set; }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 2013-03-25
    • 2020-10-08
    • 1970-01-01
    • 2023-04-05
    • 2016-07-07
    • 1970-01-01
    • 2016-03-05
    相关资源
    最近更新 更多