【发布时间】: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字符串,但没有对其进行任何操作。你能解释一下addons和postData的用途吗?您是否尝试发布这些字符串中的一个或两个? -
用于内容长度计算的插件。正如你所说,我没有使用发布数据,因为我在 webrequest.create 中提到了所有内容
-
它的工作......感谢'JoshVarty'的回答。
-
我删除了我的答案,因为我认为它与你的问题无关,但现在我已经取消删除它,因为你说它有帮助。很高兴听到一切顺利。
-
您已经说明了我的问题的确切解决方案。您修改的代码一切正常。
标签: c# asp.net oauth-2.0 google-oauth