如何在Windows应用程序C#.net中获取Google plus访问令牌

我正在准备一个Windows应用程序,因为我想使用oAuth2来访问来自google plus的信息。

但我对以下代码的要求不好。我可以在谷歌控制台中创建应用程序并获取应用程序访问的“代码”。

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( responseFromServer); 

现在,我正在尝试使用该代码来获取访问令牌。 .which正在给我BAD请求。

我还跟踪了一些关于stackoverflow的post。 但他们都没有为我工作。

谢谢

编辑:

谢谢回复。 我能够以某种方式自己做:)

我想你可以这样开始:

谷歌加OAuth

对于网络应用程序,我强烈建议您使用Google+快速入门示例中演示的一次性代码流程。 请尝试使用快速入门说明,确保您没有遗漏任何步骤。 当您以这种方式进行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上看到过这种情况。

我能够使用以下代码自己完成它。

  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用于从谷歌获取CODE,button2使用该代码并获取access_token,这是我正在寻找的。