简体   繁体   English

从AccountManager获取基本的Google身份验证令牌

[英]Obtaining a basic google auth-token from AccountManager

I want to obtain a Google Authtoken from the AccountManager that I can send to my Webservice (not hosted on App Engine) to authenticate the User (I just need the email address and eventually his name, if no permission is required for this). 我想从AccountManager获取Google身份验证令牌,然后将其发送到我的Web服务(不在App Engine上托管)以验证用户身份(如果不需要此权限,我只需要电子邮件地址,最后是他的名字)。

What do I have to use for the "authTokenType" Paramter of the "getAuthToken" method? “ getAuthToken”方法的“ authTokenType”参数必须使用什么?

And which google Api do I have to use to get the Users Email? 我必须使用哪个Google Api来获取用户电子邮件?

This is doable using OpenID Connect, however it's sort of experimental, so details could change in the future. 使用OpenID Connect可以做到这一点,但是这是试验性的,因此将来可能会更改细节。 If you get an OAuth token for the 'https://www.googleapis.com/auth/userinfo.email' or 'https://www.googleapis.com/auth/userinfo.profile' scope you can use it to get user info from https://www.googleapis.com/oauth2/v1/userinfo (including email). 如果您为“ https://www.googleapis.com/auth/userinfo.email”或“ https://www.googleapis.com/auth/userinfo.profile”范围获得OAuth令牌,则可以使用它来获取来自https://www.googleapis.com/oauth2/v1/userinfo的用户信息(包括电子邮件)。 Of course the user needs to authorize this. 当然,用户需要对此进行授权。

You should theoretically be able to get the token from AcccountManager using the "oauth2:https://www.googleapis.com/auth/userinfo.profile" as the token type, but that doesn't appear to work on my device (Galaxy Nexus with stock 4.0.4). 从理论上讲,您应该可以使用“ oauth2:https://www.googleapis.com/auth/userinfo.profile”作为令牌类型从AcccountManager获取令牌,但是在我的设备上似乎无法正常运行(Galaxy连结股票4.0.4)。 Since getting a token via the AccountManager doesn't work (at least for now), the only reliable way is to use a WebView and get one via the browser as described here: https://developers.google.com/accounts/docs/MobileApps 由于无法通过AccountManager获取令牌(至少到目前为止),唯一可靠的方法是使用WebView并通过浏览器获取令牌,如下所述: https : //developers.google.com/accounts/docs / MobileApps

There is a demo web app here that does this: https://oauthssodemo.appspot.com 这里有一个演示Web应用程序可以执行此操作: https : //oauthssodemo.appspot.com

(late) Update: Google Play Services has been released and it is the preferred way to get an OAuth token. (最新)更新:Google Play服务已经发布,这是获取OAuth令牌的首选方式。 It should be available on all devices with Android 2.2 and later. 它应在所有装有Android 2.2及更高版本的设备上可用。 Getting a profile token does work with it, in fact they use it in the demo app 获取配置文件令牌确实可以使用它,实际上他们在演示应用程序中使用了它

I have had problems with this as well, since I was not able to find anything like a reference. 我也遇到了问题,因为找不到类似参考的东西。 Perhaps this can help you (code copied from an Android example on using the account manager): 也许这可以为您提供帮助(从使用帐户管理器的Android示例中复制的代码):

  1. Somewhere in an event handler of your Android app, issue a request for an auth token to get the user's email address in Android: 在您的Android应用程序的事件处理程序中的某处,发出请求auth令牌的请求,以获取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. In the callback, extract the access token: 在回调中,提取访问令牌:

     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. Make some request to your web server, supplying the access token. 向您的Web服务器提出一些请求,并提供访问令牌。

  4. On the web server, validate the access token and obtain the email address: 在Web服务器上,验证访问令牌并获取电子邮件地址:

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

    You should get something like this: 您应该得到这样的内容:

     { "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" } 

    or if something went wrong: 或出现问题:

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

You can get the User's name with the Google+ People API. 您可以使用Google+ People API获取用户的姓名。 (It will not provide the user's email address). (它将不提供用户的电子邮件地址)。

If this is OK, you can use "Know who you are on Google" as the authTokenType. 如果可以,则可以使用“知道您在Google上的身份”作为authTokenType。

There is a sample application provided by Google that demonstrates how to use the AndroidAccountManager in conjunction with the Google+ APIs. Google提供了一个示例应用程序,该应用程序演示了如何将AndroidAccountManager与Google+ API结合使用。

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

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

相关问题 Google C2DM ClientLogin:身份验证令牌的生命周期 - Google C2DM ClientLogin: Lifetime of Auth-Token 使用CountdownLatch从accountmanager获取Auth令牌 - Getting an Auth Token from accountmanager using a CountdownLatch 无效的Accountmanager身份验证令牌 - Invalid Accountmanager auth token “令牌已过期”获取Google Auth令牌 - “Token expired” Obtaining Google Auth Token 从Android中的Accountmanager获取Dropbox帐户的身份验证令牌 - Getting auth token for dropbox account from accountmanager in android 如何在AccountManager中存储和检索auth_token - How to store and retrieve auth_token to/from AccountManager 具有多种身份验证令牌类型的Android AccountManager - Android AccountManager with multiple auth token types Accountmanager Auth令牌没有clientID和clientSecret - Accountmanager Auth token whithout clientID and clientSecret 是否可以从AccountManager获取身份验证令牌而不触发“权限请求”对话框? - Is it possible to get an auth token from AccountManager without triggering the “Permission request” dialog? 手动生成单个auth-token以在常量中使用,以验证应用程序以访问数据库 - 好还是坏? - Manually generate single auth-token for use in constant, to authenticate app to access database - Good or bad?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM