我正在尝试从存储在 Android 设备中的 Google 帐户获取全新的令牌,但我得到的只是我在过去几天缓存的旧令牌。好像是缓存在手机某处,连上网请求都没有发送(我在没有上网的情况下在app里做了测试,返回的是同一个token)。
在使用frominvalidateAuthToken
获得新方法之前,我使用了该方法。请看一下:getResult
AccountManagerFuture
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;
}
一个线程通过单例实例从 Util 类调用此方法。清单具有所有必要的权限。有人知道为什么令牌没有刷新吗?