【问题标题】:Requesting more than one oauth2 scope through AccountManager in Android在 Android 中通过 AccountManager 请求多个 oauth2 范围
【发布时间】:2012-07-14 04:18:24
【问题描述】:

我需要为两个范围(来自我的 android 应用程序)请求访问令牌,https://www.googleapis.com/auth/userinfo.emailhttps://www.googleapis.com/auth/userinfo.userinfo

我想在一次调用getAuthToken 时同时获得这两种权限,但无法确定要传入authTokenType 参数的字符串。我尝试了几种合理的组合,但没有积极的结果:(

有人解决了这个问题吗?有可能吗?

【问题讨论】:

    标签: android api authentication oauth


    【解决方案1】:

    如果您需要多个 OAuth 2.0 范围,use a space-separated list

    oauth2:https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.userinfo
    

    您要求提供示例代码,因此请查看Google Docs Upload Sample 应用程序,并在此应用程序中查看this sample Android screen 中完成的身份验证流程(忽略它是关于Google Docs,它仍然首先授权)。您可以获取整个应用程序并在具有 Google API 的模拟器中运行它,或者在您的手机上运行它。授权工作流程从 buttonAuthorize 单击开始,Authorize() 并且您对此方法特别感兴趣:

    private void gotAccount(Account account)
    {
    
        Bundle options = new Bundle();
    
        accountManager.getAuthToken(
                account,                     // Account retrieved using getAccountsByType()
                "oauth2:https://www.googleapis.com/auth/userinfo.email oauth2:https://www.googleapis.com/auth/userinfo.userinfo",            // Auth scope
                //"writely",            // Auth scope, doesn't work :(
                options,                        // Authenticator-specific options
                this,                           // Your activity
                new OnTokenAcquired(),          // Callback called when a token is successfully acquired
                null);    // Callback called if an error occurs
    }
    

    用户得到这个访问请求屏幕:

    请注意,这是使用“本地”OAuth2 机制,而不是打开网络浏览器,而是使用您首次激活 Android 手机时提供的身份验证。

    还请注意,用户看到的是范围的完整 URL 而不是友好名称,我 haven't 找到了解决此问题的方法,如果您发现了,如果您能分享答案会很棒。

    【讨论】:

    • 我已经试过了,但没有成功:(,你以前用过这个吗?如果是这样,你能提供一个小代码sn-p来说明如何吗?谢谢!!
    • 我已更新我的答案以包含对示例的引用,请尝试一下。
    • 我又试了一次,但没有成功...几乎复制了整个源代码,但肯定还有其他内容...不过感觉方向是正确的,谢谢!
    • 到底发生了什么 - 您看到上面的允许/拒绝屏幕,还是出现错误?
    • 我得到一个与上面完全相同的屏幕,然后出现另一个提示,说我的帐户有问题,并提示输入我的密码...
    【解决方案2】:

    我遇到了同样的问题。 Shah 几乎是对的,但他的范围字符串是错误的。应该是

    "oauth2:<scope_url> <scope_url>"
    

    不是

    "oauth2:<scope_url> oauth2:<scope_url>"
    

    【讨论】:

      猜你喜欢
      • 2015-10-11
      • 2017-06-26
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 2021-12-04
      • 1970-01-01
      • 2012-06-19
      相关资源
      最近更新 更多