title: Valid google account OAuth 2 token in Android
body: <p>I have a problem with google account OAuth 2 tokens.
  We need token for access account information (numeric id, email, user name)
  After request getAuthToken(account, SCOPE, options, mContext, getAuthTokenCallback, null) in AccountManager, token is not available for access to account information.
  Response of HTTP request <a href="https://www.googleapis.com/plus/v1/people/me" rel="nofollow">https://www.googleapis.com/plus/v1/people/me</a> (header "Authorization: OAuth ya29.AHES6ZSuMvL3FoxqXfevYevWyEmTPOE1HXW7_Tj6l3UAN-2J7kTs0-I")</p>
  
  <pre><code>{
      "error": {
      "errors": [
          {
           "domain": "usageLimits",
           "reason": "dailyLimitExceededUnreg",
           "message": "Daily Limit Exceeded. Please sign up",
           "extendedHelp": "https://code.google.com/apis/console"
          }
      ],
      "code": 403,
      "message": "Daily Limit Exceeded. Please sign up"
      }
  }
  </code></pre>
  
  <p>Why this error hapens?
  Previously works with two types AuthSub tokens separated by spaces.(SCOPE_OLD_PERMITIONS)
  Now it not works &amp; causes java.io.IOException</p>
  
  <p>How can I get valid token?</p>
  
  <p>This is request for get token:</p>
  
  <pre><code>TCGoogleAccountsManager mng = new TCGoogleAccountsManager(this);
  mng.requestAccountOAuthToken(this, acc);
  </code></pre>
  
  <p>сlass that helps get token:</p>
  
  <pre><code>public class TCGoogleAccountsManager {
  private static final String CLIENT_SECRET = ...;
  private static final String CLIENT_ID = ...;
  private static final String SCOPE_CONTACTS_API = "cp";
  private static final String SCOPE_ANDROID_API = "android";
  private static final String SCOPE_GOOGPE_PLUS = "oauth2:https://www.googleapis.com/auth/plus.me";
  private static final String SCOPE_MY_INFO = "oauth2:https://www.googleapis.com/auth/userinfo.email";
  private static final String SCOPE_OLD_PERMITIONS = "oauth2:https://www-opensocial.googleusercontent.com/api/people/ oauth2:https://www.googleapis.com/auth/userinfo#email";
  private static final String SCOPE = SCOPE_GOOGPE_PLUS;
  private static final String COM_GOOGLE = "com.google";
  
  private AccountManager mManager;
  private OnGetOAuthTokenRequestCompletedListener mTokenRequestListener;
  
  public TCGoogleAccountsManager(Context ctx) {
      mManager = AccountManager.get(ctx.getApplicationContext());
      mTokenRequestListener = new GoogleTokenListener(
              ctx.getApplicationContext());
  }
  
  public int getAccountsNumber() {
      return mManager.getAccountsByType(COM_GOOGLE).length;
  }
  
  public Account[] getGoogleAccounts() {
      return mManager.getAccountsByType(COM_GOOGLE);
  }
  
  
  public Account getGoogleAccountByName(String name) {
      Account foundAcc = null;
      if (name != null &amp;&amp; !name.equals("")) {
          Account[] googleAccounts = mManager.getAccountsByType(COM_GOOGLE);
          for (int i = 0; i &lt; googleAccounts.length; i++) {
              if (name.equals(googleAccounts[i].name)) {
                  foundAcc = googleAccounts[i];
                  break;
              }
          }
      }
      return foundAcc;
  }
  
  
  public Account getGoogleAccount(int index) {
      return getGoogleAccounts()[index];
  }
  
  public void requestAccountOAuthToken(Activity mContext, Account account) {
      try {
          final Bundle options = new Bundle();
          options.putString("client_id", CLIENT_ID);
          options.putString("client_secret", CLIENT_SECRET);
          mManager.getAuthToken(account, SCOPE, options, mContext,
                  getAuthTokenCallback, null);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  
  private AccountManagerCallback&lt;Bundle&gt; getAuthTokenCallback = new AccountManagerCallback&lt;Bundle&gt;() {
      public void run(AccountManagerFuture&lt;Bundle&gt; future) {
          try {
              final Bundle result = future.getResult();
              final String accountName = result
                      .getString(AccountManager.KEY_ACCOUNT_NAME);
              final String authToken = result
                      .getString(AccountManager.KEY_AUTHTOKEN);
              boolean success = (accountName != null &amp;&amp; authToken != null);
              if (!success) {
                  if (mTokenRequestListener != null) {
                      mTokenRequestListener.onRequestCompleted(false,
                              accountName, authToken);
                  }
              } else {
                  // refresh token. We need fresh token.
                  mManager.invalidateAuthToken(COM_GOOGLE, authToken);
                  mManager.getAuthToken(getGoogleAccountByName(accountName),
                          SCOPE, false, getAuthTokenCallbackInvalidated, null);
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  };
  
  
  private AccountManagerCallback&lt;Bundle&gt; getAuthTokenCallbackInvalidated = new AccountManagerCallback&lt;Bundle&gt;() {
      public void run(AccountManagerFuture&lt;Bundle&gt; future) {
          try {
              final Bundle result = future.getResult();
              final String accountName = result
                      .getString(AccountManager.KEY_ACCOUNT_NAME);
              final String authToken = result
                      .getString(AccountManager.KEY_AUTHTOKEN);
              boolean success = (accountName != null &amp;&amp; authToken != null);
              if (mTokenRequestListener != null)
                  mTokenRequestListener.onRequestCompleted(success,
                          accountName, authToken);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  };
  }
  </code></pre>
  
  <p>Thanks and Regards.</p>