android - 永远不要从 getAuthToken 请求中获取 AccountManager.KEY_INTENT

标签 android

在 JellyBean 设备上。

我正在关注 this请求 oauth2 token ,例如

AccountManager am = AccountManager.get(getActivity());
am.invalidateAuthToken(MY_AUTH_TOKEN_TYPE, null);
am.getAuthToken(aGoogleAccount, MY_AUTH_TOKEN_TYPE, null, this, 
    new OnTokenAcquired(), new Handler(new OnError()));

然后按照后面的代码示例进行检查:

private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
@Override
    public void run(AccountManagerFuture<Bundle> result) {
        Bundle bundle = result.getResult();
        ...
        Intent launch = (Intent) bundle.get(AccountManager.KEY_INTENT);
        if (launch != null) {
            startActivityForResult(launch, 0);
            return;
        }
    }
}

我从来没有得到 KEY_INTENT。我了解以下内容:

There may be many reasons for the authenticator to return an Intent. It may be the first time the user has logged in to this account. Perhaps the user's account has expired and they need to log in again, or perhaps their stored credentials are incorrect. Maybe the account requires two-factor authentication or it needs to activate the camera to do a retina scan. It doesn't really matter what the reason is. If you want a valid token, you're going to have to fire off the Intent to get it.

但是,getAuthToken 始终会导致权限屏幕或登录屏幕出现在代码命中 run 方法之前,此时 token 有效。我试过:

  1. 开启两步验证。在 run 之前请求帐户登录,因此在 run 中始终有 token 。
  2. 更改服务器上的密码。在 run 之前再次请求帐户登录,因此在 run 中始终有 token 。

没有能力尝试视网膜扫描,所以有些不知所措。

EDIT 1 我遇到的问题是在某些情况下我会得到一个 KEY_INTENT 所以我宁愿现在而不是什么时候测试这个代码路径它在外地。

提前致谢。

彼得。

最佳答案

有机会在项目中做类似的事情。这与您的代码不完全相同,我仍然说回调文档有太多“可能”无法确定它应该如何工作,但如果您使用 this methodnotifyAuthFailure 传递 false,如果您更改密码或启用 2FA,您将获得重新登录屏幕的 Intent 。这是针对 ClientLogin 的,但对于 OAuth 2(虽然未测试)应该类似地工作。像这样的东西:

// using Calendar ClientLogin for simplicity
Bundle authResult = am.getAuthToken(account, "cl", false, null, null).getResult();
if (authResult.containsKey(AccountManager.KEY_INTENT)) {
  Intent authIntent = authResult.getParcelable(AccountManager.KEY_INTENT);
  // start activity or show notification
}

关于android - 永远不要从 getAuthToken 请求中获取 AccountManager.KEY_INTENT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12970481/

相关文章:

android - 使用 Intent 标志 "CLEAR_TOP"和 launchMode ="singleTask"有什么区别?

java - 如何忽略列表中的重复项并改为添加一项?

android - 更新 SDK 后 Cordova 模拟不在 Genymotion 上运行

c# - 卡在 Xamarin AAPT 错误上

java - 从 android 服务器加载 gridView 中的图像

来自多对多数据库关系的 Android ListView

android - 通过 adb 从 android 设备中提取应用程序数据库

android - 如何在更新字段值时触发 firestore 云功能?

java - 计算估计的剩余下载时间

android - 工具栏图标的圆形背景