Hi I'm having issues recovering my authToken when I call
mAccountManager.blockingGetAuthToken(Auth.getAccount(), Auth.AUTH_TOKEN_TYPE, true)
I get a null string back, which lead me to look into my AbstractAccountAuthenticator class, specifically getAuth(). Here's what its doing:
public Bundle getAuthToken(AccountAuthenticatorResponse response,
Account account, String authTokenType, Bundle options)
throws NetworkErrorException {
final AccountManager am = AccountManager.get(mContext);
String authToken = am.peekAuthToken(account, authTokenType);
String uid = am.getUserData(account, AccountManager.KEY_CALLER_UID);
// return bundle with authToken
if (!TextUtils.isEmpty(authToken)) {
final Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
result.putString(AccountManager.KEY_CALLER_UID, uid);
return result;
}
return null;
}
The peekAuthToken is returning a null, however I am getting the correct uid
from getUserData which lead me to believe I am adding the account correctly. This is how I set the authToken:
mAccountManager.addAccountExplicitly(account, accountPassword, extraData);
//The addAccount is working, and I can obtain the extraData in getAuth
mAccountManager.setAuthToken(account, Auth.AUTH_TOKEN_TYPE, authtoken);
//I assume this is where the authToken is to be cached…but I can't retrieve it…
//The token does exist at this point
Any suggestions?
As you can read in the documentation, the peek method only gets the authToken from the authtoken-cache. If this is returning null, it means your authtoken has been invalidated, because otherwise the method AccountManager#getAuthToken would have returned you the cached one.
This is a bit confusing, but I'll try to explain.
You should be aware of that the getAuthToken from the AccountManager IS NOT the same as the getAuthToken-Method in the authenticator. The AccountManager is doing some caching in between. Means that if you call getAuthToken on the Manager, it'll return your AuthToken as long as it is in the cache WITHOUT calling the getAuthToken method of the Authenticator.
For my understanding that means, that it is totally pointless to call peek inside the getAuthToken Method.
How I handle this now:
In the implementation of getAuthToken (in the authenticator), I am re-requesting the authtoken from the server and update the account with the new token, which will store them in the cache. No need for peeking on that part.
Make sure you add
setAccountAuthenticatorResult(authIntent.getExtras());
setResult(RESULT_OK,authIntent);
once you set authToken from your code.
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