净谷歌的OAuth令牌的WebRequest错误的请求协议错误 [英] .Net Google OAuth token WebRequest Bad Request Protocol Error
本文介绍了净谷歌的OAuth令牌的WebRequest错误的请求协议错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很好与此OAuth2的东西,直到我设法得到令牌。
我觉得我做的是与编码。
下面是我的code:
字符串的URL =https://accounts.google.com/o/oauth2/token;
StringBuilder的postDataBuider =新的StringBuilder();
postDataBuider.AppendLine(code =+ code);
postDataBuider.AppendLine(CLIENT_ID =+ System.Configuration.ConfigurationManager.AppSettings [GoogleApplicationClientId]的ToString());
postDataBuider.AppendLine(client_secret =+ System.Configuration.ConfigurationManager.AppSettings [GoogleApplicationClientSecret]的ToString());
postDataBuider.AppendLine(redirect_uri =+ System.Configuration.ConfigurationManager.AppSettings [的AppDomain] +帐号/ YouTubeOAuth2Callback);
postDataBuider.AppendLine(grant_type = authorization_ code);
字符串postDataStr = postDataBuider.ToString();
// byte []的postDataBytes = System.Text.Encoding.GetEncoding(应用程序/ x-WWW的形式urlen codeD)的GetBytes(postDataStr);
byte []的postDataBytes = System.Text.Encoding.UTF8.GetBytes(postDataStr);
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(URL);
request.Accept =应用/ JSON;
request.Method =POST;
request.ContentType =应用/的X WWW的形式urlen codeD;
request.ContentLength = postDataBytes.Length;
流式传输的数据流= request.GetRequestStream();
dataStream.Write(postDataBytes,0,postDataBytes.Length);
dataStream.Close();
字符串responseStr =;
使用(HttpWebResponse响应=(HttpWebResponse)request.GetResponse())
{
使用(流responseStream = response.GetResponseStream())
{
使用(StreamReader的readStream =新的StreamReader(responseStream,Encoding.UTF8))
{
responseStr = readStream.ReadToEnd();
如果(String.IsNullOrWhiteSpace(responseStr))
{
抛出新的异常(应答为空或为空);
}
}
}
}
下面是我记录有关错误的信息:
远程服务器返回错误:(400)错误的请求。
响应状态:ProtocolError
响应头:缓存控制=无缓存,无存储,最大年龄= 0,必重新验证
响应头:杂注= no-cache的
响应头:过期=星期五,1990 1月1日00:00:00 GMT
响应头:日期=星期二2012年5时55分54秒格林尼治标准时间5月22日
响应头:内容类型=应用程序/ JSON
响应头:X-Content-Type的-选项= nosniff
响应头:X帧选项= SAMEORIGIN
响应头:X-XSS-保护= 1;模式=块
响应头:服务器= GSE
响应头:传输编码=分块
解决方案
我不知道,如果你仍然面临这个问题,但是你的要求的身体应该是在同一直线上(改变的StringBuilder。 AppendLine
为 StringBuilder.Append
),并通过分离参数和放大器;
StringBuilder的postDataBuider =新的StringBuilder();
postDataBuider.Append(code =+这个code.Text。);
postDataBuider.Append(与&的client_id =+ ClientID的);
postDataBuider.Append(与& client_secret =+ clientSecret);
postDataBuider.Append(与& redirect_uri =+ redirectUri);
postDataBuider.Append(&放大器; grant_type = authorization_ code);
这些改变后,你的code工作得很好,并访问令牌返回。
I'm doing fine with this OAuth2 stuff until I try to get the token.
I think I'm doing something to do with the encoding.
Here is my code:
string url = "https://accounts.google.com/o/oauth2/token";
StringBuilder postDataBuider = new StringBuilder();
postDataBuider.AppendLine("code=" + code);
postDataBuider.AppendLine("client_id=" + System.Configuration.ConfigurationManager.AppSettings["GoogleApplicationClientId"].ToString());
postDataBuider.AppendLine("client_secret=" + System.Configuration.ConfigurationManager.AppSettings["GoogleApplicationClientSecret"].ToString());
postDataBuider.AppendLine("redirect_uri=" + System.Configuration.ConfigurationManager.AppSettings["AppDomain"] + "Account/YouTubeOAuth2Callback");
postDataBuider.AppendLine("grant_type=authorization_code");
string postDataStr = postDataBuider.ToString();
// byte[] postDataBytes = System.Text.Encoding.GetEncoding("application/x-www-form-urlencoded").GetBytes(postDataStr);
byte[] postDataBytes = System.Text.Encoding.UTF8.GetBytes(postDataStr);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "application/json";
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataBytes.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(postDataBytes, 0, postDataBytes.Length);
dataStream.Close();
string responseStr = "";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
{
responseStr = readStream.ReadToEnd();
if (String.IsNullOrWhiteSpace(responseStr))
{
throw new Exception("Response is null or empty");
}
}
}
}
Here is the info I've logged about the error:
The remote server returned an error: (400) Bad Request.
Response Status: ProtocolError
Response Header: Cache-Control = no-cache, no-store, max-age=0, must-revalidate
Response Header: Pragma = no-cache
Response Header: Expires = Fri, 01 Jan 1990 00:00:00 GMT
Response Header: Date = Tue, 22 May 2012 05:55:54 GMT
Response Header: Content-Type = application/json
Response Header: X-Content-Type-Options = nosniff
Response Header: X-Frame-Options = SAMEORIGIN
Response Header: X-XSS-Protection = 1; mode=block
Response Header: Server = GSE
Response Header: Transfer-Encoding = chunked
解决方案
I don't know if you are still facing this issue, but your Request body should be in the same line (changed StringBuilder.AppendLine
for StringBuilder.Append
), and the parameters separated by "&":
StringBuilder postDataBuider = new StringBuilder();
postDataBuider.Append("code=" + this.code.Text);
postDataBuider.Append("&client_id=" + clientId);
postDataBuider.Append("&client_secret=" + clientSecret);
postDataBuider.Append("&redirect_uri=" + redirectUri);
postDataBuider.Append("&grant_type=authorization_code");
After these changes, your code works just fine and the access token is returned.
这篇关于净谷歌的OAuth令牌的WebRequest错误的请求协议错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文