Android account manager does not cache authToken

Hi, I am having trouble restoring my authToken when I call

mAccountManager.blockingGetAuthToken(Auth.getAccount(), Auth.AUTH_TOKEN_TYPE, true)

I get a null string back, which makes me look into my AbstractAccountAuthenticator class, in particular getAuth (). Here is what he does:

 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; } 

PeekAuthToken returns zero, however I get the correct uid from getUserData, which makes me think that I am adding the account correctly. This is how I installed 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?

+6
source share
3 answers

As you can read in the documentation, the peek method only gets authToken from the authtoken cache. If this returns null, it means your authtoken was invalidated, because otherwise the AccountManager # getAuthToken method returned the cached file to you.

This is a bit confusing, but I will try to explain.

You should be aware that the getAuthToken from AccountManager IS does NOT match the getAuthToken-Method in the authenticator. AccountManager does some caching between them. This means that if you call getAuthToken in the Manager, it will return your AuthToken while it is in the cache WITHOUT calling the getAuthToken method for Authenticator.

In my understanding, this means that it is completely pointless to call peek inside the getAuthToken method.

How am I dealing with this right now:

In the getAuthToken implementation (in the authenticator), I re-request authtoken from the server and update the account with a new token that will store them in the cache. There is no need to look into this part.

+3
source

Make sure you add

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

after you installed authToken from your code.

+1
source

This can happen if you declare your authenticator using android:customTokens=true .

You can read more in AbstractAccountAuthenticator docs .

0
source

Source: https://habr.com/ru/post/974520/


All Articles