Android Account Manager not caching the authToken
Asked Answered
U

3

6

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?

Uphill answered 28/8, 2014 at 22:51 Comment(3)
Are you sure the account variable that gets passed is that same as the one when you call setAuthToken?Williswillison
I believe so, Auth.getAccount() returns AccountManager.get(App.getContext()).getAccountsByType(ACCOUNT_TYPE)[0]; And it correctly holds the info about the accountUphill
I can't really figure out what happen by look at this code fragment. But you can check out this link for accountmanager's complete coverage. udinic.wordpress.com/2013/04/24/…Williswillison
L
4

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.

Legnica answered 17/6, 2015 at 9:26 Comment(2)
To simplify my life I wrote a library. If you're using rxjava AND retrofit (<2.0.0), you could consider using it. github.com/andretietz/retroauthLegnica
updated this library using retrofit2 without rxjava as dependencyLegnica
M
2

Make sure you add

setAccountAuthenticatorResult(authIntent.getExtras());
setResult(RESULT_OK,authIntent);

once you set authToken from your code.

Myronmyrrh answered 14/9, 2015 at 6:45 Comment(0)
H
0

This can happen if you declare your Authenticator with android:customTokens=true.

You can read more in the AbstractAccountAuthenticator docs.

Hyperbolism answered 30/3, 2017 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.