how to get Google plus access token in windows application C#.net

丶灬走出姿态 提交于 2019-12-04 22:07:31

i think you can begin this way :

Google plus OAuth

For web applications, I would strongly recommend you use a one-time-code flow as demonstrated in the Google+ Quickstart sample. Please try working through the Quickstart instructions to make sure you're not missing any steps. When you do Google+ Sign-In this way, you will be able to get over-the-air installs of Android apps (if you have one for your site) and will be applying best practices for authorization.

All of the code for doing this is available in the sample which also demonstrates integration with the Google client libraries - this opens up access to all of the Google APIs and product integrations.

From Windows apps or installed apps, you would need to do more of the heavy lifting yourself. The following blog article covers how you could do the authorization in legacy scenarios:

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

There is an example as well:

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

A couple notes:

  1. When you create your client ID, make sure it's for an installed application.
  2. The authorization code is taken from the window title after the user authorizes; this is a little dodgy and you should be doing this in a window you host in your app.
  3. Performing authorization this way will not allow you to have over-the-air installs to Android. For doing this, you could possibly host a webview inside of the application and use that for a one-time-code flow but I have never seen this working from Windows.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!