无法通过 request.GetResponse() 获取 oauth2 令牌
我正在尝试使用 GData 和 OAuth2.0 服务器端 (检查此链接),我完成了第一步并获得了第一个代码,当尝试发布请求以获取 oauth2_token 时,我总是收到错误“远程服务器返回错误:(400) 错误请求。” 这是我用来发布返回 OAuth2_token 的请求的代码:
string clientToken = Request.QueryString["code"];
string post =
string.Format(
@"code={0}&client_id={1}&client_secret={2}&redirect_uri=http://localhost/default.aspx&grant_type=authorization_code",
clientToken, Settings.ClientId, Settings.ClientSecret);
WebRequest httpRequest = WebRequest.Create("https://accounts.google.com/o/oauth2/token");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
StreamWriter streamWriter = new StreamWriter(httpRequest.GetRequestStream());
streamWriter.Write(post);
streamWriter.Flush();
streamWriter.Close();
var ss = (HttpWebResponse)httpRequest.GetResponse();
Stream stream = ss.GetResponseStream();
有帮助吗???我花了两天时间试图解决这个问题,但没有成功:(
I'm Trying to access Google Data (Contact, Edit profile data, Calendar ... etc) by using GData and OAuth2.0 server side (Check this link), I finished the first step and got the first code, and when try to post a request to get the oauth2_token I always got the error "The remote server returned an error: (400) Bad Request."
Here is the code I use to POST the request that returns the OAuth2_token:
string clientToken = Request.QueryString["code"];
string post =
string.Format(
@"code={0}&client_id={1}&client_secret={2}&redirect_uri=http://localhost/default.aspx&grant_type=authorization_code",
clientToken, Settings.ClientId, Settings.ClientSecret);
WebRequest httpRequest = WebRequest.Create("https://accounts.google.com/o/oauth2/token");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
StreamWriter streamWriter = new StreamWriter(httpRequest.GetRequestStream());
streamWriter.Write(post);
streamWriter.Flush();
streamWriter.Close();
var ss = (HttpWebResponse)httpRequest.GetResponse();
Stream stream = ss.GetResponseStream();
Any help??? I spent 2 days till now trying to solve it but in vain :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
难道redirect_uri需要进行URI编码吗?
[https://groups.google.com/forum/#!topic/oauth2-dev/9xnn8nUIA2s]
could it be that the redirect_uri needs to be URI encoded?
[https://groups.google.com/forum/#!topic/oauth2-dev/9xnn8nUIA2s]
我认为您应该使用
HttpUtility.UrlEncode
对 redirect_uri 参数进行编码。另外,您应该使用 Utf8 编码对请求正文进行编码:
希望这会有所帮助。
I think you should encode the redirect_uri parameter using
HttpUtility.UrlEncode
.also, you should encode the request body using Utf8 encoding:
hope this helps.