我正在使用 C# (ASP.NET)。我想使用 Google OAuth 访问我的应用程序中的用户个人资料详细信息。我成功获得了授权码,但在获取访问令牌时遇到了问题。我更喜欢谷歌教程。在教程中,我读到我必须发送请求并从谷歌获得响应。为此,我使用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
是授权码。