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?