如何在 Windows 应用程序 C#.net 中获取 Google plus 访问令牌 [英] how to get Google plus access token in windows application C#.net

查看:24
本文介绍了如何在 Windows 应用程序 C#.net 中获取 Google plus 访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am preparing a windows application in that I want to use oAuth2 to access info from google plus.

但是我对以下代码的请求很糟糕..我能够在谷歌控制台中创建应用程序并获取应用程序访问的代码".

But I am getting bad request for my following code.. I am able to create app in google console and fetch the "code" for application access.

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

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

Now, I am trying to use that code to get the access token. .which is giving me BAD request.

我也关注了一些关于 stackoverflow 的帖子.但他们都没有为我工作.

I also followed few post on stackoverflow. but none of them working for me.

谢谢

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

Thanks for the reply. I was able to do it my own somehow :)

推荐答案

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

I was able to do it my own using following code..

    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.

I am assuming to have two button on window.. button1 is used to get CODE from google and button2 uses that code and get the access_token which is what I was looking for.

这篇关于如何在 Windows 应用程序 C#.net 中获取 Google plus 访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆