Search code examples
androidtokenaccountmanager

AbstractAccountAuthenticator getAuthToken not being called


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.


Solution

  • 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.