【发布时间】:2012-08-03 17:15:41
【问题描述】:
我正在使用 C# (ASP.NET)。我想使用 Google OAuth 访问我的应用程序中的用户个人资料详细信息。我成功获得了授权码,但在获取访问令牌时遇到了问题。
我更喜欢Google tutorials。在教程中,我读到我必须发送请求并从谷歌获得响应。为此,我使用System.Net.HttpWebRequest/HttpWebResponse(我是否走对了路)。我用过这段代码...
byte[] buffer = Encoding.ASCII.GetBytes("?code=" + code + "&client_id=xxx&client_secret=xxx&redirect_uri=xxxx&grant_type=authorization_code");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://accounts.google.com");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = buffer.Length;
Stream strm = req.GetRequestStream();
strm.Write(buffer, 0, buffer.Length);
strm.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Response.Write(((HttpWebResponse)resp).StatusDescription);
但是,我得到了错误:
远程服务器返回错误:(405) Method Not Allowed。
更新:这里的变量code是授权码。
【问题讨论】:
-
@user854301 我可以参考这个,但我想知道
HttpWebRequest/Response的使用是否正确?我可以从HttpWebRequest向谷歌发送请求吗? -
你的缓冲区中的“代码”是什么??
-
@Apoorva 这是一个授权码。
-
如何获得授权码...你能告诉我我不知道吗..
标签: c# asp.net google-api oauth-2.0