android – AbstractAccountAuthenticator getAuthToken not being called-ThrowExceptions

Exception or error:

I am using a custom implementation of AbstractAccountAuthenticator and I have implemented getAuthToken as per the documentation. I have been following this tutorial: http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/

I am not however understanding when the getAuthToken method that is in the implementation of the AbstractAccountAuthenticator is ever called. I have set it up in the manifest with the service I created that implements it, created the login page and can add accounts, add tokens and get tokens from my code on activities using the AccountManager class but nowhere in any of the documentation explains when the AbstractAccountAuthenticator code is called.

Could someone please help me understand why I had to implement the autheniticator when it does not seem to be used.

How to solve:

That’s because there is a mediator between your application and your authenticator, and it is Android’s AccountManager class.

Your authenticator is actually a “plugin” for the account type it is associated with; it can be used by not only your application, but any other application that needs authentication for the particular account type your authenticator handles.

So when you set up your authenticator in your app’s manifest, that authenticator gets registered with the AccountManager. Now if an app authenticates with a “com.google” account type, the AccountManager will use Google’s pre-registered authenticator. If it authenticates with an account type of “com.yourapp.account”, the AccountManager will use your authenticator instead.

You app asks the AccountManager for an auth token, and it turns around and forwards the request to your authenticator.

Re-read Udini’s article. The code examples demonstrate how it all fits together.

###

in AuthenticatorActivity.java

private void finishLogin(Intent intent) {
    Log.d("udinic", TAG + "> finishLogin");

    String accountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
    String accountPassword = intent.getStringExtra(PARAM_USER_PASS);
    final Account account = new Account(accountName, intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE));

    if (getIntent().getBooleanExtra(ARG_IS_ADDING_NEW_ACCOUNT, false)) {
        Log.d("udinic", TAG + "> finishLogin > addAccountExplicitly");
        String authtoken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
        String authtokenType = mAuthTokenType;

        // Creating the account on the device and setting the auth token we got
        // (Not setting the auth token will cause another call to the server to authenticate the user)
        mAccountManager.addAccountExplicitly(account, accountPassword, null);
        mAccountManager.setAuthToken(account, authtokenType, authtoken);
    } else {
        Log.d("udinic", TAG + "> finishLogin > setPassword");
        mAccountManager.setPassword(account, accountPassword);
    }

    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

mAccountManager.setAuthToken(account, authtokenType, authtoken); have set the token. you can delete the line, and add a new account, then call getAuthToken, the method will be called.

###

Setting android:customTokens=”true” in the authenticator.xml enabled the call to getAuthToken method in my implementation of abstractaccountauthenticator class

Leave a Reply

Your email address will not be published. Required fields are marked *