Sending POST request to get OAuth Acess Token

扶醉桌前 提交于 2020-03-26 07:20:12

问题


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

来源:https://stackoverflow.com/questions/14651493/sending-post-request-to-get-oauth-acess-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!