Google Calendar API - 错误请求 (400) 尝试交换访问令牌的代码 [英] Google Calendar API - Bad Request (400) Trying To Swap Code For Access Token

查看:31
本文介绍了Google Calendar API - 错误请求 (400) 尝试交换访问令牌的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Google 获得用于访问用户日历的授权代码后,我现在正尝试将其换成访问令牌.根据他们自己的文档:

Having got an authorisation code from Google for accessing a user's calendar, I'm now trying to swap this for an access token. According to their own docs:

实际请求可能如下所示:

The actual request might look like:

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

我尝试访问它如下(C#):

My attempt to access this is as follows (C#):

string url = "https://accounts.google.com/o/oauth2/token";
WebRequest request = HttpWebRequest.Create(url); 
request.Method = "POST"; 
request.ContentType = "application/x-www-form-urlencoded"; 

string body = "code=<the_code_I_received>&
"
    + "client_id=<my_client_id>&
"
    + "client_secret=<my_client_secret>&
"
    + "redirect_uri=http://localhost:4300
"
    + "grant_type=authorization_code&
"
                            ;
byte[] bodyBytes = Encoding.ASCII.GetBytes(body); 
request.ContentLength = bodyBytes.Length ; 
Stream bodyStream = request.GetRequestStream(); 
bodyStream.Write(bodyBytes, 0, bodyBytes.Length); 
bodyStream.Close(); 

try 
{
    request.GetResponse(); 

'http://localhost:4300' 与我在原始请求中输入的完全相同(它是有效的,因为我通过在该端口上作为 Web 服务器侦听获取了代码),但我也尝试过'http://localhost' 以防万一.

'http://localhost:4300' is exactly the same as I put in the original request (and it was valid because I got the code back through listening as a web server on that port), but I also tried just 'http://localhost' just in case.

我尝试了许多建议,例如将代理设置为 null(无更改)和更改 Accept(不允许将该标头添加到 Web 请求中).

I tried a number of suggestions such as setting the proxy to null (no change) and changing Accept (wasn't allowed to add that header to the web request).

在每种情况下,我都会收到 HTTP 400 - 错误的请求(try/catch 引发异常,说明了这一点).

In each case I get the HTTP 400 - bad request back (the try / catch fires with an exception stating that).

在/token 后面放一个斜杠(我会尝试任何事情!)导致 500 内部服务器错误,所以也不是.

Putting a trailing slash after /token (I'll try anything!) resulted in a 500 Internal Server Error, so that wasn't it either.

知道我做错了什么吗?

推荐答案

你需要正文中的新行 吗?这段代码对我有用...

Do you need the new lines in the body? This code works for me...

var req0 = WebRequest.Create("https://accounts.google.com/o/oauth2/token");
req0.Method = "POST";
string postData = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code",
code, //the code i got back
"2xxx61.apps.googleusercontent.com", "XJxxxFy",
"http://localhost:1599/home/oauth2callback"); //my return URI

byte[] byteArray = Encoding.UTF8.GetBytes(postData);
req0.ContentType = "application/x-www-form-urlencoded";
req0.ContentLength = byteArray.Length;
using (Stream dataStream = req0.GetRequestStream())
{
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
}
try
{
using (WebResponse response = req0.GetResponse())
    {
    using (var dataStream = response.GetResponseStream())
        {    
        using (StreamReader reader = new StreamReader(dataStream))
        {
         string responseFromServer = reader.ReadToEnd();
         var ser = new JavaScriptSerializer();
            accessToken = ser.DeserializeObject(responseFromServer);
        }
    }
}
}
catch (WebException wex){ var x = wex; }
catch (Exception ex){var x = ex;}

这篇关于Google Calendar API - 错误请求 (400) 尝试交换访问令牌的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆