Getting basic google authenticator from AccountManager

I want to get Google Authtoken from AccountManager, which I can send to my web service (not hosted by App Engine) to authenticate the user (I just need an email address and, ultimately, his name, if permission is not required for this) .

What do I need to use for the "authTokenType" parameter of the "getAuthToken" method?

And which google api do i need to use to get users email?

+5
source share
3 answers

This can be done using OpenID Connect, but it is more experimental, so the details may change in the future. If you get the OAuth token for the https://www.googleapis.com/auth/userinfo.email or https://www.googleapis.com/auth/userinfo.profile area, you can use it to get user information from https: //www.googleapis.com/oauth2/v1/userinfo (including email). Of course, the user must authorize this.

AcccountManager, "oauth2: https://www.googleapis.com/auth/userinfo.profile" , , , (Galaxy Nexus 4.0.4). AccountManager ( ), - WebView , : https://developers.google.com/accounts/docs/MobileApps

-, : https://oauthssodemo.appspot.com

() : Google Play Services , OAuth. Android 2.2 . ,

+4

, . , (, Android ):

  • - Android- , 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);
    
  • :

    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.
            }
        }
    }
    
  • -, .

  • - :

    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"
    }
    
+2

API Google+ People. ( ).

, ", Google", authTokenType.

Google, , AndroidAccountManager API Google+.

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

0

All Articles