Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 546
Aniruddh Ambarkar Avatar asked Sep 25 '26 18:09

Aniruddh Ambarkar


1 Answers

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>);
like image 140
ffleandro Avatar answered Sep 27 '26 09:09

ffleandro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!