Search code examples
androidandroid-2.3-gingerbreadgoogle-authentication

Android google authentification userinfo.email is not working


I have a strange issue. I am using google authentication and this works on android > 3.x

But I tested on Samsung Handled (android version 2.3.3) and userinfo.email scope didn't work.

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

But after some trials, I found how to make it to work. Just needed to swap scopes:

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

This worked until I upgraded to android 2.3.5 version. After that, I couldn't get token with right for grab user email. I tried swap using only email, but nothing worked.

My code:

private static final String SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email";

    am.getAuthToken(account, SCOPE, null, this, new AccountManagerCallback<Bundle>() {
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                Bundle result = future.getResult();                 
                String token = result.getString(AccountManager.KEY_AUTHTOKEN);
                Log.d(TAG, "Google tokenas: " + token);


                authWithToken("google", token); //here I get working token, but this token don't have rights to grab user email
            } catch (OperationCanceledException e) {
                unsuccess();
            } catch (AuthenticatorException e) {
                unsuccess();
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, new Handler(new Handler.Callback() {

        public boolean handleMessage(Message msg) {
            Log.d(TAG, "on error: " + msg.what);
            return false;
        }

    }));

This method is still working on android 4.0.4 and 4.1.x.


Solution

  • I found problem. Problem is cached access token. Android don't check access token validation. If access token is invalid I just clear cache and try authentication again:

            am.invalidateAuthToken("com.google", token);
            auth(lastAccount);