AccountManager: invalidateAuthToken does not invalidate the token

戏子无情 提交于 2019-12-01 21:20:16

To invalidate the auth token you need to pass the token you want to invalidate as the second argument to invalidateAuthToken. Please see section "4.4.3 Invalidate the auth token" of this blog post. The video on that page is also helpful.

The documentation for invalidateAuthToken mention that the second argument may be null, but this only mean that it's allowed to call this method with null, not that every cached token is invalidated if null is passed.

If you do something like this instead your code should work:

// Get token
AccountManagerFuture<Bundle> future = am.getAuthToken(accounts[0], "ah", null, activity, null, null);
Bundle bundle = future.getResult();
String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);

// invalidate the token since it may have expired.
am.invalidateAuthToken("com.google", authToken);

// Get token again
future = am.getAuthToken(accounts[0], "ah", null, activity, null, null);
bundle = future.getResult();
authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);

It is simpler to simply "peek" the currently cached token, check if it is still valid, and create a new one if required.

String authToken = accountManager.peekAuthToken(account, Constants.AUTHTOKEN_TYPE);
// validate the token, invalidate and generate a new one if required
accountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
accountManager.blockingGetAuthToken(account,
                Constants.AUTHTOKEN_TYPE, NOTIFY_AUTH_FAILURE);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!