无法通过 request.GetResponse() 获取 oauth2 令牌

发布于 2024-11-19 20:47:23 字数 1139 浏览 5 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染柒℉ 2024-11-26 20:47:23

难道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]

黎歌 2024-11-26 20:47:23

我认为您应该使用 HttpUtility.UrlEncoderedirect_uri 参数进行编码。

另外,您应该使用 Utf8 编码对请求正文进行编码:

byte[] encoded = Encoding.UTF8.GetBytes(post);
httpRequest.ContentLength = encoded.Length

希望这会有所帮助。

I think you should encode the redirect_uri parameter using HttpUtility.UrlEncode.

also, you should encode the request body using Utf8 encoding:

byte[] encoded = Encoding.UTF8.GetBytes(post);
httpRequest.ContentLength = encoded.Length

hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文