Lester
Lester

Reputation: 283

Android AccountManager needs to overwrite AuthToken

Hi I would like to explicitly overwrite the existing AuthToken of the account manager. I tried using this snippet

Log.d(TAG, "finishRenewal");
AccountManager manager = AccountManager.get(context);
Account[] accounts = manager.getAccountsByType(AccountGeneral.ACCOUNT_TYPE);
for(Account ac : accounts){
  manager.setAuthToken(ac, AccountGeneral.ACCOUNT_TYPE, authToken);
}

But I can't seem to retrieve the AuthToken. It always give the previous AuthToken.

BTW: I am retrieving the token by this call

 AccountManagerFuture<Bundle> future = mAccountManager
    .getAuthTokenByFeatures(accountType, authTokenType, null, act, null, null,
        new AccountManagerCallback<Bundle>() {
          @Override
          public void run(AccountManagerFuture<Bundle> future) {
            Bundle bnd = null;
            try {
              bnd = future.getResult();
              final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
              final String userName = bnd.getString(AccountManager.KEY_ACCOUNT_NAME);
              Log.d(TAG, "GetTokenForAccount Bundle is " + bnd);
              if (listener != null) {
                listener.onAccountAcquired(userName, authtoken);
              }
            } catch (Exception e) {
              e.printStackTrace();
              if (listener != null) {
                listener.onFail();
              }
            }
          }
        }
        , null);

Upvotes: 0

Views: 405

Answers (1)

Marten
Marten

Reputation: 3872

My guess is that the value of authTokenType in the second code snippet doesn't equal AccountGeneral.ACCOUNT_TYPE from the first snippet.

Your code passes AccountGeneral.ACCOUNT_TYPE as the AuthTokenType when you set the auth token. Make sure you use the correct AuthTokenType, not the account type, when you set the authtoken.

The AuthTokenType is usually not the same as the account type, as they identify different things.

Upvotes: 1

Related Questions