AccountManager:invalidateAuthToken不会使令牌无效 [英] AccountManager: invalidateAuthToken does not invalidate the token

查看:187
本文介绍了AccountManager:invalidateAuthToken不会使令牌无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从存储在Android设备中的Google帐户中获取一个全新的令牌,但我得到的所有令牌都是我在过去几天一直在缓存的旧令牌。似乎它被缓存在手机的某个地方,甚至互联网请求都没有被发送(我在没有互联网连接的情况下在应用程序中进行测试,并返回相同的令牌)。

I'm trying to get a brand new token from a Google account stored in a Android device but all that I got is the same old token that I've been caching in the last days. It seems that it's cached somewhere in the phone, and even the Internet request is not being sent (I made the test in the app without Internet connection and the same token is returned).

我使用 invalidateAuthToken 方法,然后从 AccountManagerFuture <获取 getResult 的新方法/ code>。请看一下:

I used the invalidateAuthToken method before getting a new one with getResult from AccountManagerFuture. Take a look please:

public String updateToken(Activity activity) throws Exception {             
    AccountManager am = AccountManager.get(activity);
    Account[] accounts = am.getAccountsByType("com.google");

    if (accounts == null || 
        accounts.length == 0 || 
        "".equals(accounts[0].name.trim())) 
    {
        throw new Exception("Não há contas Google configuradas no smartphone.");
    } 
    else if (!"crsilva@gmail.com".equals(accounts[0].name.trim()) && 
             !"cristiano.bezerra@sulamerica.com.br".equals(accounts[0].name.trim()) &&
             !"tholiver@gmail.com".equals(accounts[0].name.trim())) 
    {
        Log.w("Util.updateToken","conta obtida: " + accounts[0].name);
        throw new Exception("Conta Google não autorizada.");
    }
    Log.w("Util.updateToken","conta obtida: " + accounts[0].name);
    am.invalidateAuthToken("com.google", null);
    Log.w("Util.updateToken","Passou do invalidateAuthToken");
    AccountManagerFuture<Bundle> future = 
        am.getAuthToken(accounts[0], "ah", null, activity, null, null);
    Log.w("Util.updateToken","Passou do getAuthToken");
    Bundle bundle = future.getResult();
    Log.w("Util.updateToken","Passou do getResult");
    future = null;
    am = null;
    accounts = null;
    String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
    Log.w("Util.updateToken","Token: " + authToken);
    return authToken;
}

由singleton实例从Util类调用此方法的线程。清单具有所有必需的权限。有人知道为什么令牌没有刷新?

A thread called this method from a Util class by singleton instance. The manifest has all the permissions necessary. Does somebody have idea why the token is not refreshed?

推荐答案

要使身份验证令牌无效,您需要传递您想要的令牌invalidate作为invalidateAuthToken的第二个参数。请参阅的4.4.3使授权令牌无效部分这篇博文。该页面上的视频也很有用。

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.

invalidateAuthToken 提到第二个参数可能为null,但这只表示允许调用此方法如果传递null,则不会使每个缓存的令牌失效。

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);

这篇关于AccountManager:invalidateAuthToken不会使令牌无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆