2012-08-06 135 views
11

我正在使用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是授權碼。

+1

你爲什麼不使用https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2 – user854301 2012-08-06 17:54:46

+0

@ user854301我可以參考這一點,但我想知道使用'HttpWebRequest/Response'是否正確?我可以從'HttpWebRequest'發送請求到谷歌嗎? – Sagar 2012-08-06 19:39:36

+0

什麼是在你的緩衝區中的「代碼」? – Apoorva 2012-10-11 06:17:52

回答

8

我想您發送POST請求到錯誤的端點,正確的是https://accounts.google.com/o/oauth2/token

+2

我使用這個,但它顯示我的其他錯誤。現在錯誤是'遠程服務器返回一個錯誤:(400)錯誤的請求。「我哪裏錯了? – Sagar 2012-08-08 18:44:44

+0

深入研究異常的細節,完整的錯誤信息會告訴你什麼是你的請求有問題 – 2012-08-08 18:58:50

4

正如我曾在谷歌實施的權威性過程中類似的問題,我會後,工程的代碼..最後提到的問題:錯誤(400)錯誤的請求可能由導致'?'引起在上面的代碼..

string codeClient = "code="+ t +"&client_id=number.apps.googleusercontent.com&"; 
string secretUri = "client_secret=yoursecret&" + "redirect_uri=path&" 
     + "grant_type=authorization_code"; 
postString = codeClient + secretUri; 

string url = "https://accounts.google.com/o/oauth2/token"; 

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url.ToString()); 
request.Method = "POST"; 
request.ContentType = "application/x-www-form-urlencoded"; 

UTF8Encoding utfenc = new UTF8Encoding(); 
byte[] bytes = utfenc.GetBytes(postString); 
Stream os = null; 
try 
{ 
     request.ContentLength = bytes.Length; 
     os = request.GetRequestStream(); 
     os.Write(bytes, 0, bytes.Length); 
} 
catch 
{ } 

try 
{ 
     HttpWebResponse webResponse = (HttpWebResponse) request.GetResponse(); 
     Stream responseStream = webResponse.GetResponseStream(); 
     StreamReader responseStreamReader = new StreamReader(responseStream); 
     result = responseStreamReader.ReadToEnd();//parse token from result 
+0

什麼是「t」用於代碼..以及如何得到它.. – 2015-07-10 11:34:28

+0

t初始授權代碼..​​. – 2015-09-25 19:09:02

+0

它仍然說這個代碼的錯誤請求 – 2017-06-27 12:20:08

2

我的代碼工作,我在上面兩行中犯了錯誤。它應該是這樣的

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("https://accounts.google.com/o/oauth2/token"); 

剩餘的代碼是正確的。

+2

代碼中的代碼變量是什麼.. – 2015-07-10 04:00:14

+0

@Viktor:我注意到你的**客戶端ID **和**客戶端祕密**都包含在緩衝區中。我託管的網站沒有SSL。你知道Google是否會接受來自非SSL連接的網絡請求嗎?很明顯,這不是安全的,有人可以狙擊我的憑據,但我不知道解決方法。 – jp2code 2015-09-08 22:03:53

0

最初的請求似乎有些過時。但是我發現Google的代碼示例包含很多「最佳實踐」管家代碼,這些代碼很難與基本操作分離。

我最近發佈了一個文檔,它將所有REST操作都表示爲curl命令。每種語言都很難懂,但捲曲看起來很普遍。大多數人都知道,否則,這很容易掌握。在我的捲曲示例中,-d標誌指示POST操作。否則,參數將附加到URL。

http://www.tqis.com/eloquency/googlecalendar.htm

0
public string ReceiveTokenGmail(string code, string GoogleWebAppClientID, string GoogleWebAppClientSecret, string RedirectUrl) 
{ 
    string postString = "code=" + code + "&client_id=" + GoogleWebAppClientID + @"&client_secret=" + GoogleWebAppClientSecret + "&redirect_uri=" + RedirectUrl; 

    string url = "https://accounts.google.com/o/oauth2/token"; 

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString()); 
    request.Method = "POST"; 
    request.ContentType = "application/x-www-form-urlencoded"; 

    UTF8Encoding utfenc = new UTF8Encoding(); 
    byte[] bytes = utfenc.GetBytes(postString); 
    Stream os = null; 
    try 
    { 
     request.ContentLength = bytes.Length; 
     os = request.GetRequestStream(); 
     os.Write(bytes, 0, bytes.Length); 
    } 
    catch 
    { } 
    string result = ""; 

    HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse(); 
    Stream responseStream = webResponse.GetResponseStream(); 
    StreamReader responseStreamReader = new StreamReader(responseStream); 
    result = responseStreamReader.ReadToEnd(); 

    return result; 
} 
+0

如何從Google獲取驗證碼?我想使用你的代碼,所以我不必使用key.json文件,只是使用應用程序配置爲我的客戶端ID和客戶端secret.If你可以分享如何獲得授權代碼,這將是很棒的。 – CyberNinja 2017-07-05 12:21:44

相關問題