2013-05-30 41 views
-1

我正在準備一個Windows應用程序,因爲我想使用oAuth2從Google plus中訪問信息。如何在windows應用程序中獲取Google plus訪問令牌C#.net

但我越來越不好要求我的下面的代碼..我能夠在谷歌控制檯中創建應用程序,並獲取應用程序訪問的「代碼」。

WebRequest request = WebRequest.Create(
    GoogleAuthenticationServer.Description.TokenEndpoint 
); 

     // You must use POST for the code exchange. 
     request.Method = "POST"; 

     // Create POST data. 
     string postData = FormPostData(code); 
     byte[] byteArray = Encoding.UTF8.`enter code here`GetBytes(postData); 

     // Set up the POST request for the code exchange. 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.ContentLength = byteArray.Length; 
     Stream dataStream = request.GetRequestStream(); 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     dataStream.Close(); 

     // Perform the POST and retrieve the server response with 
     // the access token and/or the refresh token. 
     WebResponse response = request.GetResponse(); 
     dataStream = response.GetResponseStream(); 
     StreamReader reader = new StreamReader(dataStream); 
     string responseFromServer = reader.ReadToEnd(); 
     reader.Close(); 
     dataStream.Close(); 
     response.Close(); 

     // Convert the response JSON to an object and return it. 
     return JsonConvert.DeserializeObject<OAuthResponseObject>(
      responseFromServer); 

現在,我試圖使用該代碼來獲取訪問令牌。這是給我不良的要求。

我也在stackoverflow上發表了幾篇文章。但他們都沒有爲我工作。

感謝

編輯:

感謝您的答覆。我能夠做我自己莫名其妙:)

回答

0

我能夠做我自己的使用下面的代碼..

private void button2_Click(object sender, EventArgs e) 
    { 
     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token"); 
     webRequest.ContentType = "application/x-www-form-urlencoded"; 
     webRequest.Method = "POST"; 
     authLink.AppendFormat("code={0}", code); 
     authLink.AppendFormat("&client_id={0}", "996688211762.apps.googleusercontent.com"); 
     authLink.AppendFormat("&client_secret={0}", "nprfJuBUOyU2hsb3tqt1XDnB"); 
     authLink.AppendFormat("&redirect_uri={0}", "urn:ietf:wg:oauth:2.0:oob"); 
     authLink.Append("&grant_type=authorization_code"); 
     UTF8Encoding utfenc = new UTF8Encoding(); 
     byte[] bytes = utfenc.GetBytes(authLink.ToString()); 
     Stream os = null; 
     try // send the post 
     { 
      webRequest.ContentLength = bytes.Length; // Count bytes to send 
      os = webRequest.GetRequestStream(); 
      os.Write(bytes, 0, bytes.Length);  // Send it 
     } 
     catch (Exception ex) { MessageBox.Show(ex.Message); } 
     try // get the response 
     { 
      HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); 
      if (webResponse == null) { MessageBox.Show("null"); } 
      StreamReader sr = new StreamReader(webResponse.GetResponseStream()); 
      textBox1.Text = sr.ReadToEnd().Trim(); 
      //MessageBox.Show(sr.ReadToEnd().Trim()); 
     } 
     catch (Exception ex) { MessageBox.Show(ex.Message); } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     StringBuilder authLink = new StringBuilder(); 
     authLink.Append("https://accounts.google.com/o/oauth2/auth"); 
     authLink.AppendFormat("?scope={0}", "https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile"); 
     authLink.AppendFormat("&client_id={0}", "xxxxxx.apps.googleusercontent.com"); 
     authLink.AppendFormat("&redirect_uri={0}", "urn:ietf:wg:oauth:2.0:oob"); 
     authLink.Append("&response_type=code"); 
     string u = @"https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=xxxxxxxx.apps.googleusercontent.com"; 
     webBrowser1.Navigate(u); 
    } 

我假設有對窗口中的兩個按鈕..button1用於從谷歌獲取代碼,button2使用該代碼並獲取我正在尋找的access_token。

+0

嘿感謝您的答案。我們爲自己的項目修改了哪些參數?只有應用程序ID和客戶端密碼? – MonsterMMORPG

+0

我想你不能使谷歌+發佈到您的流與此? – MonsterMMORPG

+0

什麼是按鈕click2中的「code」變量 –

0

對於Web應用程序,我會強烈建議您使用一次性碼流爲在Google+ Quickstart sample中演示。請嘗試通過快速入門說明來確保您不會錯過任何步驟。當您以這種方式進行Google+登錄時,您可以通過無線安裝Android應用(如果您的網站有一個),並將採用最佳做法進行授權。

這樣做的所有代碼都可以在示例中找到,它還演示了與Google客戶端庫的集成 - 這樣可以訪問所有Google API和產品集成。

從Windows應用程序或已安裝的應用程序中,您需要自己完成更多繁重工作。下面的博客文章介紹,你怎麼能這樣做的授權在傳統的場景:

http://gusclass.com/blog/2012/08/31/using-the-google-net-client-library-with-google/

有一個例子,以及:

http://gusclass.com/projects/PlusDotNet.zip

一對夫婦的注意事項:

  1. 當您創建您的客戶端ID時,請確保它適用於已安裝的應用程序。
  2. 授權碼取自用戶授權後的窗口標題;這是一個有點狡猾,你應該在你的應用程序託管的窗口中這樣做。
  3. 以這種方式執行授權不會讓您無線安裝到Android。爲此,您可能會在應用程序內部放置一個webview並將其用於一次性代碼流,但我從未在Windows上看到過這種方式。
+0

我收到了BAD REQUEST錯誤,正如我的問題中所解釋的。 – butanijayanti

+0

是否有任何樣本是爲桌面應用程序實現的?而不是Web應用程序? – butanijayanti

相關問題