C# 无法从Google OAuth2令牌终结点获取访问令牌

C# 无法从Google OAuth2令牌终结点获取访问令牌,c#,asp.net,oauth,oauth-2.0,google-oauth,C#,Asp.net,Oauth,Oauth 2.0,Google Oauth,我正试图从Google的授权端点检索访问令牌,但是我一直得到401:Unauthorized 我非常确定发送的所有值(授权码、客户端id、客户端机密、重定向uri和授权类型)都是正确的 我的代码如下: using (HttpClient client = new HttpClient()) { IEnumerable<KeyValuePair<string,string>> data = new List<KeyValuePair<string,str

我正试图从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);
}
然而,我正在从我的Google开发者控制面板复制和粘贴客户id和客户机密,所以他们绝对不会错


有什么帮助吗?

这是对我有用的。在这个示例中,我使用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);

当您将凭据传递到OAuth2.0平台时,是否仍会出现错误?你在这方面有什么进展吗?@vtortola我很抱歉。在我找到解决方案之前,这个项目就中断了。如果你们碰巧发现了它,把它贴在这里,因为我仍然对学习很感兴趣!我希望这有助于:
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);
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; }
    }