【问题标题】:Unable to get response with WebRequest of method post无法通过方法 post 的 WebRequest 获得响应
【发布时间】:2013-02-08 20:46:58
【问题描述】:

我想使用谷歌的 OAUTH2.0 对访问我网站的用户进行身份验证。

我已成功获得授权码作为响应,现在当我在获取访问令牌时向 Google 发出“POST 请求”时遇到问题。问题是请求一直在持续,我没有得到任何响应。

我下面写的代码有什么问题吗?

StringBuilder postData = new StringBuilder();
postData.Append("code=" + Request.QueryString["code"]);
postData.Append("&client_id=123029216828.apps.googleusercontent.com");
postData.Append("&client_secret=zd5dYB9MXO4C5vgBOYRC89K4");
postData.Append("&redirect_uri=http://localhost:4180/GAuth.aspx&grant_type=authorization_code");

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token?code=" + Request.QueryString["code"] + "&client_id=124545459218.apps.googleusercontent.com&client_secret={zfsgdYB9MXO4C5vgBOYRC89K4}&redirect_uri=http://localhost:4180/GAuth.aspx&grant_type=authorization_code");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string addons = "/o/oauth2/token?code=" + Request.QueryString["code"] + "&client_id=123029216828.apps.googleusercontent.com&client_secret={zd5dYB9MXO4C5vgBOYRC89K4}&redirect_uri=http://localhost:4180/GAuth.aspx&grant_type=authorization_code";

request.ContentLength = addons.Length;

Stream str = request.GetResponse().GetResponseStream();
StreamReader r = new StreamReader(str);
string a = r.ReadToEnd();

str.Close();
r.Close();

【问题讨论】:

  • 您正在构建您的 postData 字符串,但没有对其进行任何操作。你能解释一下addonspostData 的用途吗?您是否尝试发布这些字符串中的一个或两个?
  • 用于内容长度计算的插件。正如你所说,我没有使用发布数据,因为我在 webrequest.create 中提到了所有内容
  • 它的工作......感谢'JoshVarty'的回答。
  • 我删除了我的答案,因为我认为它与你的问题无关,但现在我已经取消删除它,因为你说它有帮助。很高兴听到一切顺利。
  • 您已经说明了我的问题的确切解决方案。您修改的代码一切正常。

标签: c# asp.net oauth-2.0 google-oauth


【解决方案1】:

正如我在评论中提到的,您的代码中只有一些小错误。就目前而言,您实际上并没有发布任何内容。我假设您打算发送postData 字符串。

以下应该有效:

//Build up your post string
StringBuilder postData = new StringBuilder();
postData.Append("code=" + Request.QueryString["code"]);
postData.Append("&client_id=123029216828.apps.googleusercontent.com");
postData.Append("&client_secret=zd5dYB9MXO4C5vgBOYRC89K4");
postData.Append("&redirect_uri=http://localhost:4180/GAuth.aspx&grant_type=authorization_code");

//Create a POST WebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token?code=" + Request.QueryString["code"] + "&client_id=124545459218.apps.googleusercontent.com&client_secret={zfsgdYB9MXO4C5vgBOYRC89K4}&redirect_uri=http://localhost:4180/GAuth.aspx&grant_type=authorization_code");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

//Write your post string to the body of the POST WebRequest
var sw = new StreamWriter(request.GetRequestStream());
sw.Write(postData.ToString());
sw.Close();

//Get the response and read it
var response = request.GetResponse();
var raw_result_as_string = (new StreamReader(response.GetResponseStream())).ReadToEnd();

您只是缺少将字符串附加到 POST WebRequest 的部分。

【讨论】:

    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多