title: How to get rid of java.lang.IllegalStateException while trying to getAuthToken from Account
body: <p>I am trying to get the <code>authToken</code> for an account but getting this error:</p>
  
  <pre><code>java.lang.IllegalStateException: calling this from your main thread can lead to deadlock
  </code></pre>
  
  <p>This is how I'm getting the AuthToken:</p>
  
  <pre><code>public class MyAccount {
   private final Account account;
   private final AccountManager manager;
   private final Context context;
  
   public MyAccount (Context context) {
          this.context = context;
          final Account[] accounts = AccountManager.get(context).getAccountsByType("com.myapp");
          account = accounts.length &gt; 0 ? accounts[0] : null;
          manager = AccountManager.get(context);
   }
  
      public String getAuthToken() {
          @SuppressWarnings("deprecation")
          AccountManagerFuture&lt;Bundle&gt; future = manager.getAuthToken(account,"com.myapp", false, null, null);
  
          try {
              //GETTING EXCEPTION HERE
              Bundle result = future.getResult();
              return result != null ? result.getString(KEY_AUTHTOKEN) : null;
          } catch (AccountsException e) {
              Log.e(TAG, "Auth token lookup failed", e);
              return null;
          } catch (IOException e) {
              Log.e(TAG, "Auth token lookup failed", e);
              return null;
          }
      }
  }
  </code></pre>
  
  <p>I'm calling this from my MainActivity like this:</p>
  
  <pre><code> MyAccount account = new MyAccount(getApplicationContext());
   String authtoken = account.getAuthToken());
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>How can I call <code>MyAccount</code> to get the authToken and get rid of the exception?</p>