C# Google日历API-尝试将代码交换为访问令牌的错误请求(400)

C# Google日历API-尝试将代码交换为访问令牌的错误请求(400),c#,google-calendar-api,access-token,bad-request,C#,Google Calendar Api,Access Token,Bad Request,从谷歌获得了访问用户日历的授权码,我现在正试图用它来交换访问令牌。根据他们自己的文件: 实际请求可能如下所示: 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&

从谷歌获得了访问用户日历的授权码,我现在正试图用它来交换访问令牌。根据他们自己的文件:

实际请求可能如下所示:

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

stringurl=”https://accounts.google.com/o/oauth2/token";
WebRequest=HttpWebRequest.Create(url);
request.Method=“POST”;
request.ContentType=“application/x-www-form-urlencoded”;
string body=“code=&\r\n”
+“客户端\u id=&\r\n”
+“客户端\u机密=&\r\n”
+"重定向"uri=http://localhost:4300\r\n“
+“授权类型=授权代码&\r\n”
;
byte[]bodyBytes=Encoding.ASCII.GetBytes(body);
request.ContentLength=bodyBytes.Length;
Stream bodyStream=request.GetRequestStream();
bodyStream.Write(bodyBytes,0,bodyBytes.Length);
bodyStream.Close();
尝试
{
request.GetResponse();
"http://localhost:4300'与我在原始请求中输入的代码完全相同(这是有效的,因为我通过在该端口上作为web服务器进行侦听获得了代码),但我也尝试了'http://localhost“以防万一

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

在每种情况下,我都会返回HTTP 400-bad请求(try/catch会触发一个异常声明)

在/token后面加上一个尾随斜杠(我什么都可以试试!)会导致500个内部服务器错误,所以也不是这样


你知道我做错了什么吗?

你需要正文中的新行吗\r\n?这段代码适合我

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