androidandroid-accountandroid-authenticatorauth-token

Cannot get AuthToken for custom account from different app


I have two apps that work with a same account type. I want below page to be shown when the user opens the second app for first time and one account exists:

enter image description here

But nothing happens when I run this code:

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

The above code works correctly when I run it from the app that has the authenticator. When I run below code instead, system generates a notification that when I click on it, the above picture appears.

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

Clicking allow button returns the AuthToken correctly. However I want to see the grant permission page (above picture) when calling getAuthToken, not by clicking on notification. How can I do that?


Solution

  • I used this method instead of previous one and now I see the confirmation dialog:

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

    Note that the second app must have different signature. If both apps have a same signature, no confirmation is required and authToken will retrieve.