简体   繁体   English

google oauth访问令牌411响应

[英]google oauth access token 411 response

I'm working with google's OAuth api for web server applications, specifically asp.net mvc, and i'm able to get to the point where google returns an authorization code for a certain oauth request. 我正在使用适用于Web服务器应用程序的Google OAuth api,特别是asp.net mvc,并且可以使Google返回某个oauth请求的授权码。 At that point, I'm trying to obtain the access token using the following code: 那时,我正在尝试使用以下代码获取访问令牌:

public ActionResult GetOAuthToken()
{
     HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(OAuthConfig.getTokenUrl(Request.QueryString["code"].ToString()));
     myReq.Method = "POST";
     myReq.Host = "accounts.google.com";
     myReq.ContentType = "application/x-www-form-urlencoded";
     WebResponse resp = myReq.GetResponse();
     return View();
 }

The OAuthConfig is just a class I wrote that contains a method getTokenUrl(), which returns a url with parameters such as code, client_secret, client_id etc. for the url: https://accounts.google.com/o/oauth2/token . OAuthConfig只是我编写的一个类,其中包含方法getTokenUrl(),该方法返回带有参数的网址,例如网址的代码,client_secret,client_id等: https : //accounts.google.com/o/oauth2/token I've debugged and checked that there's nothing wrong with this url. 我已调试并检查此URL是否没有问题。

I keep getting the following error: The remote server returned an error: (411) Length Required. 我不断收到以下错误:远程服务器返回错误:(411)所需长度。

I don't know what to specify for the content length, or if there's something else that i need to include to fix this error? 我不知道要为内容长度指定什么,或者是否还需要包含其他内容来解决此错误?

Have you tried to have a look at Google-API .NET Client ? 您是否尝试过查看Google-API .NET客户端

If you debug you will see Google uses "length" as an internal property when sending access-token request. 如果您进行调试,则将在发送访问令牌请求时看到Google使用“长度”作为内部属性。 you can try to fix it on your own, but you can use THEIR class in order to send the token request; 您可以尝试自己修复它,但是可以使用THEIR类来发送令牌请求; If you use their class you do not have to worry about internal such as length... 如果您使用他们的课程,则不必担心诸如长度之类的内部...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM