【问题标题】:how to get Google plus access token in windows application C#.net如何在 Windows 应用程序 C#.net 中获取 Google plus 访问令牌
【发布时间】:2013-05-26 01:06:30
【问题描述】:

我正在准备一个 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<OAuthResponseObject>(
            responseFromServer);

现在,我正在尝试使用该代码来获取访问令牌。 .这给了我不好的请求。

我还关注了一些关于 stackoverflow 的帖子。但他们都不为我工作。

谢谢

编辑:

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

【问题讨论】:

    标签: c# google-api google-plus access-token bad-request


    【解决方案1】:

    我认为你可以这样开始:

    Google plus OAuth

    【讨论】:

    • 这不是访问令牌的最佳方式
    【解决方案2】:

    对于 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 上这样工作。

    【讨论】:

    • 有没有为桌面应用程序实现的示例......不是网络应用程序?
    【解决方案3】:

    我可以使用以下代码自己完成。

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

    【讨论】:

    • 嘿,谢谢你的回答。那么我们为自己的项目修改哪些参数呢?只有应用程序 ID 和客户端密码?
    • 我想你不能用这个在你的信息流中发布谷歌+帖子?
    猜你喜欢
    • 2015-02-22
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 2016-07-17
    相关资源
    最近更新 更多