JeB
JeB

Reputation: 12123

Failing to retrieve OAuth 2.0 access token on android emulator

I'm trying to login into my application using GoogleAccountCredential for the authentication:

mGoogleAccountCredential = GoogleAccountCredential.usingOAuth2(context, Arrays.asList(Scopes.EMAIL, Scopes.PLUS_LOGIN));
mGoogleAccountCredential.setSelectedAccountName(accountName);
String token = mGoogleAccountCredential.getToken();

It works just fine on real devices, but on the android emulator mGoogleAccountCredential.getToken() fails with the following exception:

java.lang.IllegalArgumentException: the name must not be empty: null
03-01 19:41:31.604 3203-3361/com.myapp W/System.err:     at android.accounts.Account.<init>(Account.java:48)
03-01 19:41:31.604 3203-3361/com.myapp  W/System.err:     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
03-01 19:41:31.604 3203-3361/com.myapp  W/System.err:     at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)

Any clue why isn't it working on the emulator?

UPD:
After digging a bit in Google's code: the issue occurs in setSelectedAccountName(accountName) method. This method asks GoogleAccountManager to give him an account associated with the given account name. If there is no such an account, the account name is being set to null:

  public final GoogleAccountCredential setSelectedAccountName(String accountName) {
    selectedAccount = accountManager.getAccountByName(accountName);
    // check if account has been deleted
    this.accountName = selectedAccount == null ? null : accountName;
    return this;
  }

AccountManager, in turn, goes over all the existing account and compares their names to the given account name. If there is a match, the appropriate account is returned:

  public Account getAccountByName(String accountName) {
    if (accountName != null) {
      for (Account account : getAccounts()) {
        if (accountName.equals(account.name)) {
          return account;
        }
      }
    }
    return null;
  }

  public Account[] getAccounts() {
    return manager.getAccountsByType("com.google");
  }

The thing is that getAccounts() returns empty array on the emulator. On a real device, however, it returns a proper list.

Upvotes: 5

Views: 1726

Answers (3)

JeB
JeB

Reputation: 12123

Well, as always things are easier than they seem.
Thanks to this post and to b1izzar for pointing to the right answer.

All the real devices I checked on are running Android 5.1 Lollipop.
All the emulators I checked on are running Android 6.0 Marshmallow.

On Marshmallow, i.e. on my emulator, it's not enough to specify GET_ACCOUNTS permission in manifest. It is mandatory to request this permission at runtime with a specific code:

Request permissions:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

Note: in Marshmallow GET_ACCOUNTS, WRITE_CONTACTS and READ_CONTACTS permissions are in the same permission group, so once READ_CONTACTS is granted, GET_ACCOUNTS is granted as well.

Note 2: in Android Nougat GET_ACCOUNTS is deprecated, so it makes sense to use READ_CONTACTS instead of GET_ACCOUNT even in Marshmallow.

Upvotes: 2

Gustavo Morales
Gustavo Morales

Reputation: 2670

I think that the problem is that you must use a physical device for developing and testing because Google Play services cannot be installed on an emulator.

I don't see another reason, but here you have a code snippet taken from tasks-android-sample that also use GoogleAccountCredential.

Upvotes: 1

Ryan Ford
Ryan Ford

Reputation: 326

Perhaps the emulator is running an older version of Google Services. It appears that the latest version would throw GoogleAuthException as opposed to IllegalArgumentException.

API Doc

public String getToken()
                throws IOException,
                       com.google.android.gms.auth.GoogleAuthException
Returns an OAuth 2.0 access token.
Must be run from a background thread, not the main UI thread.

Throws:
IOException
com.google.android.gms.auth.GoogleAuthException

Upvotes: 1

Related Questions