Question

I created my own Android account authenticator by extending AbstractAccountAuthenticator and implementing addAccount() and getAuthToken(). Some of the methods in it are called by AccountManager, but others are not.

This works great:

AccountManager#addAccount()

AccountManager accountManager = AccountManager.get(activity);
accountManager.addAccount(MyAccountAuthenticator.ACCOUNT_TYPE,
    MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS, null, null,
    activity, callback, null);

The problem happens when I call AccountManager#getAuthToken() in my Activity. The AccountManager does not call the getAuthToken() method I define in my AccountAuthenticator. It calls some other default method that only checks for existence of an authToken before starting the AuthenticatorActivity.

This does not work. It does not call my getAuthToken() method:

AccountManager#getAuthToken()

AccountManager accountManager = AccountManager.get(activity);
accountManager.getAuthToken(
        mAccount, MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS, null,
        activity, callback, handler);

AuthenticatorService

I created my service and defined onBind(). addAccount() should not work otherwise.

public IBinder onBind(Intent intent) {
    return intent.getAction().equals(ACTION_AUTHENTICATOR_INTENT) ? new MyAccountAuthenticator(this).getIBinder() : null;
}

EDIT: I call addAccountExplicitly in MyAuthenticatorActivity after the app gets an auth token back for the user.

Snippet from class MyAuthenticatorActivity extends AccountAuthenticatorActivity:

if (getIntent().getBooleanExtra(KEY_IS_ADDING_NEW_ACCOUNT, false)) {
    // Creating the account on the device and setting the auth token we recieved
    accountManager.addAccountExplicitly(account, null, null);
}
Was it helpful?

Solution

Your comment cleared things up immensely -- if you set the auth token for the account, then your getAuthToken method will not be called until the token is invalidated. You generally do this by calling invalidateAuthToken upon receiving a 401 or 403 or what have you from the web service.

From the Javadoc for the getAuthToken methods:

If a previously generated auth token is cached for this account and type, then it is returned. Otherwise, if a saved password is available, it is sent to the server to generate a new auth token. Otherwise, the user is prompted to enter a password.

Since your token is in the cache, it is returned directly and your authenticator is not consulted.

OTHER TIPS

for calling AuthenticatorActivity in method AccountManager#getAuthToken, you must send intent to the activity by parcelable, method for example :

    final Intent intent = new Intent(mContext, LoginActivity.class);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, accountAuthenticatorResponse);
    intent.putExtra(LoginActivity.ARG_ACCOUNT_TYPE, account.type);
    intent.putExtra(LoginActivity.ARG_AUTH_TYPE, authTokenType);
    intent.putExtra(LoginActivity.ARG_ACCOUNT_NAME, account.name);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top