我想从AccountManager获取一个Google Authtoken,我可以将它发送到我的need服务(不是托管在App Engine上)来验证用户(我只需要电子邮件地址和他的名字,如果不需要权限的话)。
我必须为"getAuthToken“方法的"authTokenType”参数使用什么?
我必须使用哪个google Api才能获得用户的电子邮件?
发布于 2012-05-18 10:58:53
使用OpenID连接可以做到这一点,但这是一种实验,所以细节可能会在未来发生变化。如果你获得了'https://www.googleapis.com/auth/userinfo.email‘或'https://www.googleapis.com/auth/userinfo.profile’作用域的OAuth令牌,你就可以使用它从https://www.googleapis.com/oauth2/v1/userinfo获取用户信息(包括电子邮件)。当然,用户需要授权。
理论上,你应该能够使用"oauth2:https://www.googleapis.com/auth/userinfo.profile“作为令牌类型从AcccountManager获取令牌,但这在我的设备上似乎不起作用(Galaxy Nexus,股票为4.0.4)。由于通过AccountManager获取令牌不起作用(至少现在是这样),因此唯一可靠的方法是使用WebView并通过浏览器获取,如下所述:https://developers.google.com/accounts/docs/MobileApps
这里有一个演示web应用程序可以做到这一点:https://oauthssodemo.appspot.com
(最近)更新:谷歌Play服务已经发布,这是获取OAuth令牌的首选方式。它应该可以在Android 2.2或更高版本的所有设备上使用。获取配置文件令牌确实可以使用它,事实上,他们在演示应用程序中使用它
发布于 2012-09-15 18:47:54
我在这方面也遇到了问题,因为我找不到任何类似参考的东西。这可能会对你有所帮助(从Android示例中复制的关于使用帐户管理器的代码):
_accountMgr = AccountManager.get( this );Account [] accounts = _accountMgr.getAccounts();Account account = accounts;//对我来说,这是谷歌,仍然需要弄清楚如何通过名称获取它。_accountMgr.getAuthToken(account,AUTH_TOKEN_TYPE,false,new GetAuthTokenCallback(),null);
私有类包实现了AccountManagerCallback { public void run(AccountManagerFuture result) { bundle bundle;try { Bundle = result.getResult();final String access_token =bundle //将令牌存储在可以将其提供给web服务器的地方。} catch (Exception e) { //在这里做点什么。}
curl -d 'access_token=‘https://www.googleapis.com/oauth2/v1/tokeninfo
你应该会得到这样的东西:
{ "issued_to":".apps.googleusercontent.com","audience":".apps.googleusercontent.com","scope":"https://www.googleapis.com/auth/userinfo.email","expires_in":3562,"email":"","verified_email":true,"access_type":"online“}
或者如果出了什么问题:
{ "error":"invalid_token","error_description":“请求错误”}
发布于 2012-05-15 04:02:21
您可以使用Google+ People API获取用户名。(它不会提供用户的电子邮件地址)。
如果这样可以,你可以使用“知道你在谷歌上是谁”作为authTokenType。
谷歌提供了一个示例应用程序,演示了如何结合使用AndroidAccountManager和Google+ API。
链接:http://code.google.com/p/google-plus-java-starter/source/browse/#hg%2Fandroid
https://stackoverflow.com/questions/10565759
复制相似问题