【问题标题】:Getting access token for google+ with requested permissions使用请求的权限获取 google+ 的访问令牌
【发布时间】:2013-06-23 07:47:47
【问题描述】:

我一直在我的桌面应用程序中使用 google+ APIs for C#。我必须访问 google+ 用户的朋友列表。

我一直通过从浏览器标题中提取访问令牌来获取访问令牌。

它适用于以下权限(全部分组并同时询问)并在浏览器标题中返回身份验证代码(成功 = authcode)。

                 "https://www.googleapis.com/auth/plus.me
                 "https://www.googleapis.com/auth/youtube",
                 "https://www.googleapis.com/auth/youtube.upload" 

但是当我更改了获取好友列表的权限时,

                 "https://www.googleapis.com/auth/plus.login

它没有返回所需的验证码,它只是返回成功 authuser=0&promt...

顺便说一句,我无法在本地网络服务器上收听。 如何使用上述权限获取访问令牌?

【问题讨论】:

  • 嗨..实际上它在标题栏中返回验证码,但格式更改。通常它返回“Success=Authcode”但对于上述权限,它返回“Success authuser=0&prompt=consent&session_state =96d1b8d6c0d5d49b4e5404e4bfac620f0265c016..acb1&code=AuthCode",所以在标题栏中看不到。现在我可以从标题中提取验证码了..

标签: c# wpf google-api oauth-2.0 google-plus


【解决方案1】:

我刚刚检查了这一点,当您使用 plus.login 范围时,窗口标题中的成功字符串实际上是不同的。但是,字符串中返回的代码仍然有效。以下代码显示了一种从窗口标题字符串中提取授权代码的方法:

    string pollActiveWindowForAuthCode(int sleepTime){
        string activeTitle = GetActiveWindowTitle();
        while (!activeTitle.StartsWith("Success"))
        {
            activeTitle = GetActiveWindowTitle();
            Thread.Sleep(sleepTime);
        }
        // strip to start of auth code
        string trimToAuthCode = activeTitle.Substring(activeTitle.LastIndexOf("=") + 1);
        // trim the " - Google Chrome" text
        return trimToAuthCode.Substring(0, trimToAuthCode.IndexOf(' '));
    }

我已将更全面的演示上传到我的 GitHub:

csharp-gplus-wpf

【讨论】:

  • 谢谢@class!!这段代码就像一个魅力,我能够检索到验证码..
猜你喜欢
  • 1970-01-01
  • 2012-01-19
  • 2020-04-13
  • 1970-01-01
  • 2015-01-09
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多