android - 无法从不同的应用程序获取自定义帐户的 AuthToken

标签 android android-account android-authenticator auth-token

我有两个使用相同帐户类型的应用程序。当用户第一次打开第二个应用程序并且存在一个帐户时,我希望显示以下页面:

enter image description here

但是当我运行这段代码时没有任何反应:

final AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(account, authTokenType, null, this, null, null);

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Bundle bnd = future.getResult();

            final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
            showMessage((authtoken != null) ? "SUCCESS!\ntoken: " + authtoken : "FAIL");
            Log.d("udinic", "GetToken Bundle is " + bnd);
        } catch (Exception e) {
            e.printStackTrace();
            showMessage(e.getMessage());
        }
    }
}).start();

当我从具有身份验证器的应用程序运行上面的代码时,它可以正常工作。当我运行下面的代码时,系统会生成一个通知,当我点击它时,会出现上面的图片。

final AccountManagerFuture<Bundle> future = mAccountManager
        .getAuthToken(account, authTokenType, null, true,
                null, handler);

点击允许按钮正确返回 AuthToken。但是我想在调用 getAuthToken 时看到授予权限页面(上图),而不是通过单击通知。我该怎么做?

最佳答案

我使用了这种方法而不是以前的方法,现在我看到了确认对话框:

accountManager.getAuthToken(account, AUTH_TOKEN_TYPE_FULL_ACCESS, null, true, new AccountManagerCallback<Bundle>() {
            @Override
            public void run(AccountManagerFuture<Bundle> future) {
                try {
                    Bundle bundle = future.getResult();
                    String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);

                } catch (OperationCanceledException | IOException | AuthenticatorException e) {

                }
            }
}, null);

请注意,第二个应用程序必须具有不同的签名。如果两个应用程序具有相同的签名,则不需要确认,authToken 将检索。

关于android - 无法从不同的应用程序获取自定义帐户的 AuthToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32413724/

相关文章:

java - 如何根据日期/时间过滤 ArrayList 并将其显示在 Android Studio 的回收 View 中?

android - 如何分离必须在核心层并由Presenter触发的Interactors

php - 将应用程序连接到本地主机上的 php 服务器

android - 如何获取 Yahoo/Microsoft 帐户的电子邮件 ID

android - 在不创建帐户的情况下使用 SyncAdapter

Android Account Authenticator 编辑 Email Id Credentials

android - 当用户从设置中删除 Android Authenticator 帐户时自动注销

android - ListView 中 TextView 的文本未被省略

android - Auth.GoogleSignInApi 作为 GoogleAuthUtil 替代谷歌加号登录

安卓 : AccountPicker set Light Theme