I am trying to add a new account (after logging in to Facebook + server validation) in the AccountManager. The flow for this case is as follows:
- Facebook user login
- I received the information after the login was completed, and I check them for the data that I have on my server.
- If everything is in order, the server sends back auth_token (JWT token)
- Availability of user information and auth_token I create an account through AccountManager, and after creating it, I set authToken for it.
- At the next login, when the user opens the application again, I call getAuthToken, which will first try to get the cached authToken by calling peekAuthToken ().
Problem
At point 5, peekAuthToken returns null , but this should not be because I already set autotoken for this account.
code
public static Bundle handleUserLogin(Context context, User user) { SharedPreferences mPrefs = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE); AccountManager am = AccountManager.get(context); Account account = new Account(user.getEmail(), ACCOUNT_TYPE); Account[] accounts = am.getAccountsByType(ACCOUNT_TYPE); boolean isNewAccount = true; for (int i = 0; i < accounts.length; i++) { if (user.getEmail().equalsIgnoreCase(accounts[i].name) && ACCOUNT_TYPE.equalsIgnoreCase(accounts[i].type)) { isNewAccount = false; account = accounts[i]; break; } } if (isNewAccount) { am.addAccountExplicitly(account, user.getPassword(), null); accounts = am.getAccountsByType(ACCOUNT_TYPE); for (int i = 0; i < accounts.length; i++) { if (user.getEmail().equalsIgnoreCase(accounts[i].name) && ACCOUNT_TYPE.equalsIgnoreCase(accounts[i].type)) { account = accounts[i]; break; } } } if (null != user.getPassword()) { am.setPassword(account, user.getPassword()); } Cs.error(TAG, "account " + account + " token " + user.getToken()); am.setAuthToken(account, user.getToken(), Authenticator.AUTHTOKEN_TYPE_FULL_ACCESS); setUserData(user, account, am); 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, user.getToken()); mPrefs.edit().putString(Constants.KEY_CURRENT_USER, account.name).commit(); return result; }
At first I thought that maybe the link to my new account is incorrect (for example, from the AccountManager), so I'm looking for an account again.
Could you give me some guidance on what I am doing wrong, or how should I make sure that authToken will be set for the account?
thanks
source share