简体   繁体   English

AccountManager:invalidateAuthToken不会使令牌无效

[英]AccountManager: invalidateAuthToken does not invalidate the token

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

I used the invalidateAuthToken method before getting a new one with getResult from AccountManagerFuture . 在使用AccountManagerFuture getResult获取新的方法之前,我使用了invalidateAuthToken方法。 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;
}

A thread called this method from a Util class by singleton instance. 由singleton实例从Util类调用此方法的线程。 The manifest has all the permissions necessary. 清单具有所有必需的权限。 Does somebody have idea why the token is not refreshed? 有人知道为什么令牌没有刷新?

To invalidate the auth token you need to pass the token you want to invalidate as the second argument to invalidateAuthToken. 要使auth令牌无效,您需要将要作为invalidateAuthToken的第二个参数无效的令牌传递给invalidateAuthToken。 Please see section "4.4.3 Invalidate the auth token" of this blog post . 请参阅此博客文章 “4.4.3使auth令牌无效”部分。 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. invalidateAuthToken的文档提到第二个参数可能为null,但这仅表示允许使用null调用此方法,而不是如果传递null则每个缓存的标记都无效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Websphere上使LTPA令牌无效 - Invalidate LTPA token on Websphere 使用CountdownLatch从accountmanager获取Auth令牌 - Getting an Auth Token from accountmanager using a CountdownLatch 使用Android AccountManager获取OAuth2令牌? - Obtain OAuth2 token using android AccountManager? 使用带有accountmanager令牌的imap访问gmail - Access gmail using imap with accountmanager token Java会话无效且超时不起作用 - Java Session invalidate and timeout does not work JAVA:如何使特定用户的所有 JWT 令牌失效? - JAVA : How to invalidate all JWT token of particular user? 在MFP上注销后,我们可以使旧的访问令牌无效吗? - Can we invalidate the old access token after logout on MFP? Guava CacheLoader - 如果设置了expireAfterWrite和expireAfterAccess,则invalidate不会立即使条目无效 - Guava CacheLoader - invalidate does not immediately invalidate entry if both expireAfterWrite and expireAfterAccess are set JSP Servlet会话invalidate()不会使会话为空 - JSP Servlet session invalidate() does not make session null JPA Eclipselink数据库更改通知不会使高速缓存条目无效 - JPA Eclipselink Database Change Notification does not invalidate cache entry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM