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.
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
.
getAuthToken()
method:AccountManager#getAuthToken()
AccountManager accountManager = AccountManager.get(activity);
accountManager.getAuthToken(
mAccount, MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS, null,
activity, callback, handler);
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With