Failing to retrieve OAuth 2.0 access token on android emulator

可紊 提交于 2019-12-05 01:38:11

问题


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 the setSelectedAccountName (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.


回答1:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/35732242/failing-to-retrieve-oauth-2-0-access-token-on-android-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!