androidaccountmanagerauth-token

Android setAuthToken not working for first time


I am saving my application account on Android using AccountManager and want to save authentication token on device. My app account is added every time when I login and execute addAccountExplicitly but setAuthToken does not work untill I login one more time and setAuthToken again. Below is my code for adding account and setting authToken

String accountName = authIntent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
String accountType = authIntent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
String accountPass = authIntent.getStringExtra(Constants.ACCOUNT_PASS);
String accntAuthToken = authIntent.getStringExtra(AccountManager.KEY_AUTHTOKEN);

Account resilincAccount = new Account(accountName, Constants.ACCOUNT_TYPE);
AccountManager mAccountManager = AccountManager.get(getApplicationContext());

boolean accountAdded = mAccountManager.addAccountExplicitly(resilincAccount, accountPass, null);
mAccountManager.setAuthToken(resilincAccount, Constants.ACCOUNT_TYPE, accntAuthToken);
mAccountManager.addOnAccountsUpdatedListener(this, null, true);
setAccountAuthenticatorResult(authIntent.getExtras());
setResult(RESULT_OK, authIntent);

Any one have any idea why I have to login two times to get my authToken saved in AccountManager?


Solution

  • The Bundle that you pass to setAccountAuthenticatorResult has to contain the following properties:

    res.putExtra(AccountManager.KEY_ACCOUNT_TYPE, <account_type>);
    res.putExtra(AccountManager.KEY_ACCOUNT_NAME, <user_id>);
    res.putExtra(AccountManager.KEY_AUTHTOKEN, <auth_token>);