Android帐户管理器没有缓存authToken

AIn*_*tel 6 android auth-token

嗨,我打电话时遇到恢复我的authToken的问题

mAccountManager.blockingGetAuthToken(Auth.getAccount(), Auth.AUTH_TOKEN_TYPE, true)

我得到一个空字符串,这导致我查看我的AbstractAccountAuthenticator类,特别是getAuth().这是它的作用:

public Bundle getAuthToken(AccountAuthenticatorResponse response,
        Account account, String authTokenType, Bundle options)
        throws NetworkErrorException {

    final AccountManager am = AccountManager.get(mContext);

    String authToken = am.peekAuthToken(account, authTokenType);
    String uid = am.getUserData(account, AccountManager.KEY_CALLER_UID);


    // return bundle with authToken
    if (!TextUtils.isEmpty(authToken)) {
        final 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, authToken);
        result.putString(AccountManager.KEY_CALLER_UID, uid);
        return result;
    }


    return null;
}
Run Code Online (Sandbox Code Playgroud)

peekAuthToken返回null,但是我uid从getUserData 得到了正确的结果,这让我相信我正在添加帐户.这是我设置authToken的方式:

mAccountManager.addAccountExplicitly(account, accountPassword, extraData);
//The addAccount is working, and I can obtain the extraData in getAuth
mAccountManager.setAuthToken(account, Auth.AUTH_TOKEN_TYPE, authtoken);
//I assume this is where the authToken is to be cached…but I can't retrieve it…
//The token does exist at this point
Run Code Online (Sandbox Code Playgroud)

有什么建议?

and*_*dre 4

正如您在文档中所读到的,peek方法仅从 authtoken 缓存中获取 authToken。如果返回 null,则意味着您的 authtoken 已失效,因为否则方法AccountManager#getAuthToken会返回缓存的令牌。

这有点令人困惑,但我会尽力解释一下。

您应该注意, AccountManager 中的getAuthToken与身份验证器中的getAuthToken-Method不同。AccountManager 在两者之间进行一些缓存。意味着如果您在 Manager 上调用 getAuthToken,只要您的 AuthToken 位于缓存中,它就会返回您的 AuthToken,而无需调用 Authenticator 的 getAuthToken 方法。

根据我的理解,这意味着在 getAuthToken 方法中调用 peek 是完全没有意义的。

我现在如何处理这个问题:

在 getAuthToken (在身份验证器中)的实现中,我从服务器重新请求 authtoken 并使用新令牌更新帐户,这会将它们存储在缓存中。无需偷看那部分。