【问题标题】:.Net Google OAuth token WebRequest Bad Request Protocol Error.Net Google OAuth 令牌 WebRequest 错误请求协议错误
【发布时间】:2012-05-22 06:07:25
【问题描述】:

在我尝试获取令牌之前,我在 OAuth2 方面做得很好。

我想我正在做一些与编码有关的事情。

这是我的代码:

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");
            }
        }
    }
}

这是我记录的有关错误的信息:

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

【问题讨论】:

    标签: .net httpwebrequest oauth-2.0 content-type


    【解决方案1】:

    不知道你是否还有这个问题,但是你的Request body应该在同一行(StringBuilder.AppendLine改为StringBuilder.Append),参数用“&”隔开:

    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");
    

    在这些更改之后,您的代码可以正常工作并返回访问令牌。

    【讨论】:

    • 我已经尝试过了,但仍然失败。使用 fiddler 并使用 Postman 比较成功的帖子,意识到我的客户密码中有一个“+”号,并且在发布时它被解码为空格。解决方案是对客户端密码进行 UrlEncode。这为我解决了问题。
    猜你喜欢
    • 2013-07-21
    • 1970-01-01
    • 2012-07-26
    • 2018-10-28
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 2011-05-04
    相关资源
    最近更新 更多