Reputation: 745
I'm currently trying to implement AccountManager
to my project. Creating the account works well, I can access the AuthToken without problem.
However, once I refresh and update the AuthToken in the AccountManager
, I can't seem to access it anymore as AccountManager.getAuthToken()
returns null
every time and I end up refreshing the token every single time I try to access it.
Here's my getAuthToken()
method:
public static String getAuthToken(final Context context)
{
Log.d(TAG, "getAuthToken");
AccountManager accountManager = AccountManager.get(context);
String accountType = context.getResources().getString(R.string.account_type);
Account accounts[] = accountManager.getAccountsByType(accountType);
if (accounts.length == 0) {
return null;
}
/* This method retrieves the token for the given account but doesn't pop the Authentication Page if not found */
AccountManagerFuture<Bundle> futureBundle = accountManager.getAuthToken(accounts[0], AUTHENTICATION_TOKEN_TYPE, null, false, null, null);
Bundle bundle;
try {
bundle = futureBundle.getResult();
} catch (OperationCanceledException ex) {
Log.wtf(TAG, "operation canceled: " + ex.getMessage());
return null;
} catch (IOException ex) {
Log.wtf(TAG, "IOException: " + ex.getMessage());
return null;
} catch (AuthenticatorException ex) {
throw new RuntimeException(ex.getMessage());
}
Log.d(TAG, "AccountManagerUtility getAuthToken: " + bundle.getString(AccountManager.KEY_AUTHTOKEN));
return bundle.getString(AccountManager.KEY_AUTHTOKEN);
}
And here's the method updating the AccountManager:
public static void updateAccount(Context context, final OAuthToken authToken)
{
AccountManager accountManager = AccountManager.get(context);
String accountType = context.getResources().getString(R.string.account_type);
Account accounts[] = accountManager.getAccountsByType(accountType);
if (accounts.length == 0) {
return;
}
accountManager.setAuthToken(accounts[0], AUTHENTICATION_TOKEN_TYPE, authToken.getAccessToken());
accountManager.setPassword(accounts[0], authToken.getRefreshToken());
final long timestamp = java.lang.System.currentTimeMillis();
setRefreshTokenTimestamp(context, timestamp); // Updates User data
}
Permissions are set in the AndroidManifest
:
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
And Authenticator is also defined. Actually, after updateAccount
has been called, the Authenticator's getAuthToken
is being called and that's the method that refreshes the token.
Thanks for you help.
Upvotes: 2
Views: 3538
Reputation: 884
Use this code to get AuthToken from your App's account instead:
AccountManager accountManager=AccountManager.get(getContext());
String oAuth=accountManager.peekAuthToken(account,"oauth");
accountManager.getAuthToken(accounts[0], AUTHENTICATION_TOKEN_TYPE, null, false, null, null);
is now deprecated.
Upvotes: 1