发送POST请求以获取OAuth访问令牌

时间:2013-02-01 17:10:26

标签: c# rest windows-runtime oauth-2.0 httpclient

Google API包含有关实施OAuth 2.0的文档。但是我对你应该如何获得访问令牌感到很困惑。文档说我必须以下列格式发送POST请求

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

我不知道这是什么意思。这是否意味着我必须发送这样的帖子命令。

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

所需的params是那个巨大的字符串。有什么提示吗?

[更新

我使用上述实现时出错

   -        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

0 个答案:

没有答案