2012-05-12 75 views
5

我想从AccountManager获取一个Google Authtoken,我可以发送到我的Web服务(不是在App Engine上托管)来认证用户(我只需要电子邮件地址,最终他名称,如果没有这个要求的权限)。从AccountManager获取基本的谷歌认证令牌

我必须使用“getAuthToken”方法的“authTokenType”参数吗?

而我必须使用哪个google Api来获取用户电子邮件?

+0

刚发现另一个在Stackoverflow上似乎适合的答案:http://stackoverflow.com/a/6680837 –

回答

4

这是可行的使用OpenID连接,但是这有点实验性的,所以细节可能会在未来改变。如果您获得了“https://www.googleapis.com/auth/userinfo.email”或“https://www.googleapis.com/auth/userinfo.profile”范围的OAuth令牌,则可以使用它获取来自https://www.googleapis.com/oauth2/v1/userinfo的用户信息(包括电子邮件)。当然,用户需要对此进行授权。

您理论上应该能够使用从AcccountManager获得令牌“的oauth2:HTTPS://www.googleapis.com/auth/userinfo.profile”作为标记类型,但不会出现上下工夫我的设备(Galaxy Nexus with stock 4.0.4)。由于通过AccountManager获取令牌不起作用(至少现在),唯一可靠的方法是使用WebView并通过浏览器获取,如下所述:https://developers.google.com/accounts/docs/MobileApps

这里有一个演示Web应用程序,是否这样:https://oauthssodemo.appspot.com

(晚)更新:Google Play服务已经发布,它是获取OAuth令牌的首选方式。它应该适用于Android 2.2及更高版本的所有设备。获取个人资料令牌确实有效,实际上他们在演示应用程序中使用它

+0

是否有可用于https://www.googleapis.com/auth/userinfo.email或https://www.googleapis.com/auth/userinfo.profile的别名(否则此网址将显示在权限中请求,我不认为是非常用户友好的)? –

+0

没有我知道的。你设法得到一个令牌吗?正如我所说,至少在我的设备上看起来似乎不起作用。 –

+0

好的谢谢你的信息,非常有趣。我需要一种可以跨越所有设备的解决方案,所以这种方法对于这种方法并不合适。我现在能想到的唯一可能的解决方案是获取一个应用引擎令牌,并拥有一个额外的应用引擎后端,它将提供一个认证服务。我真的不喜欢这种方法。 –

0

您可以通过Google+人员API获取用户名。 (它不会提供用户的电子邮件地址)。

如果这样可以,您可以使用“知道您在Google上的人”作为authTokenType。

Google提供了一个示例应用程序,演示如何将Android帐户管理器与Google+ API结合使用。

链接:http://code.google.com/p/google-plus-java-starter/source/browse/#hg%2Fandroid

+0

谢谢,但我需要一个令牌,我可以用它来获取用户的电子邮件,因为这是后端识别用户的方式。 –

2

我也遇到过这个问题,因为我无法找到任何参考资料。或许,这可以帮助你(代码从Android例如复制使用该客户经理):

  1. 某处在你的Android应用程序的事件处理程序,发出一个身份验证令牌的请求,以获取用户的电子邮件地址安卓:

    _accountMgr = AccountManager.get(this); 
    Account [] accounts = _accountMgr.getAccounts();     
    Account account = accounts[0]; // For me this is Google, still need to figure out how to get it by name. 
    _accountMgr.getAuthToken(account, AUTH_TOKEN_TYPE, false, new GetAuthTokenCallback(), null); 
    
  2. 在回调,提取访问令牌:

    private class GetAuthTokenCallback implements AccountManagerCallback<Bundle> { 
        public void run(AccountManagerFuture<Bundle> result) { 
         Bundle bundle; 
         try { 
          bundle = result.getResult(); 
          final String access_token = bundle.getString(AccountManager.KEY_AUTHTOKEN); 
          // store token somewhere you can supply it to your web server. 
         } catch (Exception e) { 
          // do something here. 
         } 
        } 
    } 
    
  3. 使一些请求到Web服务器,提供了ACC ess令牌。

  4. 在Web服务器,验证访问令牌和获得的电子邮件地址:

    curl -d 'access_token=<this is the token the app sent you>' https://www.googleapis.com/oauth2/v1/tokeninfo 
    

    你应该得到这样的事情:

    { 
        "issued_to": "<something>.apps.googleusercontent.com", 
        "audience": "<something>.apps.googleusercontent.com", 
        "scope": "https://www.googleapis.com/auth/userinfo.email", 
        "expires_in": 3562, 
        "email": "<users email address>", 
        "verified_email": true, 
        "access_type": "online" 
    } 
    

    ,或者如果出了问题:

    { 
        "error": "invalid_token", 
        "error_description": "Bad Request" 
    }