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

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

问题描述

已经得到了来自谷歌的授权代码访问用户的日历,我现在想换这个访问令牌。根据自己的文档:




实际的请求可能是这样的:

  POST / O /的oauth2 /令牌HTTP / 1.1 
主机:accounts.google.com
内容类型:应用程序/ x-WWW窗体-urlencoded

码= 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#):

 字符串的URL =htt​​ps://accounts.google.com/o/oauth2/token ; 
的WebRequest请求= HttpWebRequest.Create(URL);
request.Method =POST;
request.ContentType =应用/的X WWW的形式,进行了urlencoded

绳体=暗码=< the_code_I_received>&安培; \r\\\

+的client_id =< my_client_id>&安培; \r\\\

+client_secret =< my_client_secret>&安培; \r\\\

+REDIRECT_URI = HTTP://本地主机:4300\r\\\

+grant_type = authorization_code&放; \r\\\

;
字节[] = bodyBytes Encoding.ASCII.GetBytes(体);
request.ContentLength = bodyBytes.Length;
流的BodyStream = request.GetRequestStream();
bodyStream.Write(bodyBytes,0,bodyBytes.Length);
bodyStream.Close();


{
request.GetResponse();



的http://本地主机:4300是我把原来的请求完全一样(并且它是有效的,因为我得到了代码回到通过监听作为该端口上的Web服务器),但我也尝试了的http:// localhost'的,以防万一。



我尝试了一些建议,如设置代理为空(没有变化)和改变接受(不允许的头添加到Web请求)。



在每一种情况下,我得到了HTTP 400 - 错误的请求回(在try / catch语句与火灾的异常说明)。



把尾随斜线后/令牌(我会尝试任何事情!)导致了500内部服务器错误,所以这是不是它的。



任何想法,我做错了吗?


解决方案

您是否需要新生产线在体内\r\\\
?此代码对我的作品...

  VAR请求0 = WebRequest.Create(https://accounts.google.com/ O /的oauth2 /令牌); 
req0.Method =POST;
串POSTDATA =的String.Format(码= {0}&安培; CLIENT_ID = {1}&安培; client_secret = {2}&安培; REDIRECT_URI = {3}&安培; grant_type = authorization_code,
码//我回来
2xxx61.apps.googleusercontent.com代码,XJxxxFy,
的http://本地主机:1599 /家庭/ oauth2callback); //我返回URI,

字节[]的字节数组= Encoding.UTF8.GetBytes(POSTDATA);
req0.ContentType =应用/的X WWW的形式,进行了urlencoded
req0.ContentLength = byteArray.Length;
使用(流数据流= req0.GetRequestStream())
{
dataStream.Write(字节数组,0,byteArray.Length);
dataStream.Close();
} $ B使用(WebResponse的响应= req0.GetResponse())
{
使用$ B试
{
(VAR数据流= response.GetResponseStream())
{使用
(StreamReader的读者=新的StreamReader(数据流))
{
串responseFromServer = reader.ReadToEnd();
变种SER =新的JavaScriptSerializer();
的accessToken = ser.DeserializeObject(responseFromServer);
}
}
}
}
赶上(引发WebException WEX){VAR X = WEX; }
赶上(例外前){VAR X =前;}


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

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>&\r\n"
    + "client_id=<my_client_id>&\r\n"
    + "client_secret=<my_client_secret>&\r\n"
    + "redirect_uri=http://localhost:4300\r\n"
    + "grant_type=authorization_code&\r\n"
                            ;
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' 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.

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

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

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

Any idea what I'm doing wrong?

解决方案

Do you need the new lines \r\n 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;}

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

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