3

I'm trying to retrieve an access token from Google's authorization endpoint, however I keep getting 401: Unauthorized.

I'm pretty sure all the values being sent (authorization code, client's id, client's secret, redirect uri and grant type) are correct.

My code is the following:

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);
}

The response's JSON is:

{
    "error": "invalid_client",
    "error_description": "Unauthorized"
}

However, I'm copying & pasting the client's id and client's secret from my Google Developer control panel, so there is no way they are wrong.

Any help?

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
  • Do you still get the error when you pass your credentials into the OAuth2.0 Playground? https://developers.google.com/oauthplayground/ – Andy Dec 13 '14 at 00:00
  • Did you get any progress on this? – vtortola Feb 16 '15 at 12:20
  • @vtortola I'm very sorry. This project got discontinued before I managed to find a solution. If you guys happen to find it, post it here as I'm still very interested in learning! – Matias Cicero Apr 10 '15 at 02:16
  • I hope this helps: http://stackoverflow.com/questions/28548920/googles-openidconnect-return-a-base64-token-that-cannot-be-parsed – vtortola Apr 10 '15 at 13:27

1 Answers1

0

Here is what worked for me. In this sample, I'm using the RefreshToken to get an 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);

Here is my GoogleAPIAuth class.

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; }
    }
Dumber_Texan2
  • 840
  • 2
  • 12
  • 34