0

I want to retrieve an user token. This is my code:

        AccountManager am = AccountManager.get(mActivity);

        AccountManagerFuture<Bundle> bundleAccountManagerFuture = am.getAuthTokenByFeatures(ACCOUNT_TYPE, AUTHORIZATION_TOKEN_TYPE, null, mActivity, null, null, null, null);

        try {
            Bundle result = bundleAccountManagerFuture.getResult(30, TimeUnit.SECONDS);

            if (result!=null) {
                OTTCache.getInstance().authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
            }
        } catch (OperationCanceledException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AuthenticatorException e) {
            e.printStackTrace();
        }

But the process stop in this line:

Bundle result = bundleAccountManagerFuture.getResult();

Some idea?

Thank you very much

Exception:

android.accounts.OperationCanceledException W/System.err: at android.accounts.AccountManager$AmsTask.internalGetResult(AccountManager.java:1985) W/System.err: at android.accounts.AccountManager$AmsTask.getResult(AccountManager.java:1997) W/System.err: at android.accounts.AccountManager$AmsTask.getResult(AccountManager.java:1995) W/System.err: at com.fr.apps.cliente.tv.task.LoginTask.doInBackground(LoginTask.java:83) W/System.err: at com.fr.apps.cliente.tv.task.LoginTask.doInBackground(LoginTask.java:25) W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:304) W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) W/System.err: at java.lang.Thread.run(Thread.java:761)

Patrick
  • 1,629
  • 5
  • 23
  • 44

3 Answers3

0

Using getResult() attempts to call on the variable, and the code continues immediately. I'm assuming you're trying to retrieve it from a server or something, rather than from being stored on the device? In that case you need to wait for .isDone() (android doc here) to become true before you process the result.

AD S2dios
  • 31
  • 5
0

From Documentation of AccountManagerFuture for .getResult() method -

This * call will block until the result is available. In order to check if the result is * available without blocking, one may call {@link #isDone()} and {@link #isCancelled()}.

EDIT 1: You can use getResult(long timeout, TimeUnit unit) which will timeout after a specified time if the request is blocked. After this, you can check if task was successfully done or not using .isDone() and accordingly, proceed.

Sachin Aggarwal
  • 1,095
  • 8
  • 17
  • bundleAccountManagerFuture.isDone() and bundleAccountManagerFuture.isCancelled() are false. That I have to do? – Patrick Sep 20 '17 at 08:59
  • 1
    You can use timeout which is another variation of this function. After this method, you have to check if it was done or not. Then accordingly handle the case. It will prevent program to be stuck at this statement. – Sachin Aggarwal Sep 20 '17 at 09:05
  • I put the timeout and now throw catch (OperationCanceledException e) { – Patrick Sep 20 '17 at 09:38
  • @Patrick What are you getting in message of this exception? The reason for this exception can be many. You may not have all the permission from OAuth scopes, your authentication code maybe wrong, you are modifying account you are not allowed to, etc. – Sachin Aggarwal Sep 20 '17 at 09:54
  • @Patrick I think this is related to your Auth Token. Are you sure you are getting auth token correctly? Have a look at this question as well - https://stackoverflow.com/questions/6116987/accountmanager-blockinggetauthtoken-gets-stuck – Sachin Aggarwal Sep 20 '17 at 10:15
0

I found the problem:

The package name of my application do not match exactly with the package name of my other app (This app have the login). Now, i signed both with the same key.

Patrick
  • 1,629
  • 5
  • 23
  • 44