I have weird behavior when using Android AccountManager to get authentication token for google account.
When the application starts, the first call to getAuthToken
returns a bundle with an empty string as a token. Next time I call the same method, it returns a valid token.
Here is my code:
public String updateToken(final boolean invalidateToken, final Context c) { String authToken = ""; try { final AccountManager am = AccountManager.get(c); final Account[] accounts = am.getAccountsByType("com.google"); final Bundle bundle = am.getAuthToken(accounts[0], "android", true, null, null).getResult(); authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN) .toString(); if (invalidateToken) { am.invalidateAuthToken("com.google", authToken); authToken = updateToken(false, c); } } catch (final Exception e) {
It seems like an empty token is returned when this method is called in the onCreate
method of my activity, although this is not always the case.
Thanks in advance. Also, I do not know when you need to invalidate the token. Once a day? On every run? Or is it an empty token to indicate that the token should be invalid, although it returns a valid token on the very next call.
source share