Sending POST request to get OAuth Acess Token

2019-08-29 08:03发布

问题:

Google API has documents on implementing OAuth 2.0. However I am quite confused at how your supposed to obtain the access token. The documentation says that I must send a POST request in the following format

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code

Thing is I have no idea what this is saying. Does it mean I have to send a post command like this.

  HttpClient httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("Host", "accounts.google.com");
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage tokenResponse = await httpClient.PostAsync(new Uri("https://accounts.google.com/o/oauth2/token"), new StringContent(requiredParams));

Where Required params is that giant string. Any tips?

[Update]

Error I get using the above implementation

   -        tokenResponse   {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Pragma: no-cache
  X-Content-Type-Options: nosniff
  X-Frame-Options: SAMEORIGIN
  X-XSS-Protection: 1; mode=block
  Transfer-Encoding: chunked
  Cache-Control: no-store, must-revalidate, no-cache, max-age=0
  Date: Fri, 01 Feb 2013 17:05:59 GMT
  Server: GSE
  Content-Type: application/json
  Expires: Fri, 01 Jan 1990 00:00:00 GMT
}}  System.Net.Http.HttpResponseMessage