【问题标题】:Failed To get oauth2 Token via request.GetResponse()无法通过 request.GetResponse() 获取 oauth2 令牌
【发布时间】:2011-10-04 12:41:52
【问题描述】:

我正在尝试使用 GData 和 OAuth2.0 服务器端 (Check this link) 访问 Google 数据(联系人、编辑个人资料数据、日历...等),我完成了第一步并获得了第一个代码,当尝试发布获取 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();

有什么帮助???到现在我花了 2 天时间试图解决它,但徒劳无功:(

【问题讨论】:

标签: c# asp.net gdata-api oauth-2.0


【解决方案1】:

难道redirect_uri需要进行URI编码?

[https://groups.google.com/forum/#!topic/oauth2-dev/9xnn8nUIA2s]

【讨论】:

    【解决方案2】:

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

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

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

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-24
      • 2018-12-30
      • 2021-05-20
      • 2015-02-11
      • 2016-06-22
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多