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)
- Google Play Services present on the emulator (
GoogleApiAvailability.isGooglePlayServicesAvailable(context)
returns 0) accountName
is set and correct when passed to thesetSelectedAccountName
(set to"myuser@gmail.com"
)- All the permissions, dependecies and configurations exist in the project (as a matter of fact, it works on all the real devices)
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.