我正在使用C#(ASP.NET)。我想使用Google oauth在我的應用程序中訪問用戶個人資料的詳細信息。我成功獲取授權碼,但在獲取訪問令牌方面遇到問題。 我更喜歡Google tutorials。在教程中,我讀到我必須發送請求並從Google獲得響應。爲此,我使用System.Net.HttpWebRequest/HttpWebResponse
(我是否正確)。我用這個代碼....如何獲取谷歌oauth的訪問令牌?
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);
但是,我得到了錯誤:
The remote server returned an error: (405) Method Not Allowed.
更新:這裏變量code
是授權碼。
你爲什麼不使用https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2 – user854301 2012-08-06 17:54:46
@ user854301我可以參考這一點,但我想知道使用'HttpWebRequest/Response'是否正確?我可以從'HttpWebRequest'發送請求到谷歌嗎? – Sagar 2012-08-06 19:39:36
什麼是在你的緩衝區中的「代碼」? – Apoorva 2012-10-11 06:17:52