简体   繁体   English

获取Google Plus API的OAuth访问令牌

[英]Getting OAuth access token for Google Plus API

I m current developing an app for google plus. 我目前正在为Google Plus开发一个应用。 I want to fetch the access token from google plus API. 我想从Google Plus API中获取访问令牌。 I want to use OAuth 2.0 to fetch the access token. 我想使用OAuth 2.0来获取访问令牌。 I have constructed to URL 我已经构造到URL

"https://accounts.google.com/o/oauth2/auth?client_id=752264386186- 72f3ef2ok1j3k8g12h7hg8k5kjt9s9si.apps.googleusercontent.com&scope=https: //www.googleapis.com/auth/plus.me&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code" “ https://accounts.google.com/o/oauth2/auth?client_id=752264386186- 72f3ef2ok1j3k8g12h7hg8k5kjt9s9si.apps.googleusercontent.com&scope = https://www.googleapis.com/auth/plus.me&redirect_uri=urn:ietf:wg :oauth:2.0:oob&response_type = code“

which when I pasted to by browser asks to sign in with my google account. 当我通过浏览器粘贴时,它要求使用我的Google帐户登录。 So when I enter my credentials it asks for "allow access" or "no thanks" . 因此,当我输入我的凭据时,它会要求“允许访问”或“不,谢谢”。 when I click "allow access" it gives me the access token in my browser. 当我单击“允许访问”时,它将在浏览器中提供访问令牌。 I want the above steps to be done using my C# code. 我希望使用我的C#代码完成上述步骤。

How I can navigate to authorisation page from code and fetch the access token once the authentication is sucessful. 身份验证成功后,如何从代码导航到授权页面并获取访问令牌。 Should I use httpwebrequest or webclient or rest sharp. 我应该使用httpwebrequest还是webclient还是要休息一下。 Please provide me some sample code also. 也请提供一些示例代码。

If using the WebBrowser is okay for you, here is a simple example how I did the same thing with Foursquare API. 如果您可以使用WebBrowser,这是一个简单的示例,说明了我如何使用Foursquare API进行相同的操作。 This code has been written only to test how it goes, so I'm sure it's missing several important things like error handling etc. 编写此代码只是为了测试它的运行方式,因此我确定它缺少一些重要的内容,例如错误处理等。

 private void button1_Click(object sender, RoutedEventArgs e)
    {
        string address =
            "https://foursquare.com/oauth2/authenticate" +
            "?client_id=" + CLIENT_ID +
            "&response_type=token" +
            "&redirect_uri=" + CALLBACK_URI;
        webBrowser1.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(webBrowser1_Navigated);
        webBrowser1.Navigate(new Uri(address, UriKind.Absolute));
    }

    void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        response = e.Uri.ToString();
        System.Diagnostics.Debug.WriteLine(e.Uri.ToString());
        if (response.LastIndexOf("access_token=") != -1)
        {
            string token = response.Substring(response.LastIndexOf("#access_token=") + "#access_token=".Length);
            System.Diagnostics.Debug.WriteLine("TOKEN: " + token);
        }
    }

Once you have allowed access, you end up getting the callback URI with the access_token. 允许访问后,最终将获得带有access_token的回调URI。 Note that webBrowser1_Navigated might get called several (each time you are navigating to another page in the embedded web browser). 请注意,webBrowser1_Navigated可能会被称为多次(每次您导航到嵌入式Web浏览器中的另一个页面时)。

这段代码是EPIC,真是太神奇了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM