Android – GoogleAuthUtil.getToken Exception error: Unknown

There are a lot of guides out there to do OAuth2 authentication through Google on an Android application. I’ve been reading several of them, javadocs, Stackoverflow questions and so on, trust me. Finally, after got connected to Google I wanted to retrieve the token for the user connected. For that I used the GoogleAuthUtil class. Here is a snippet of the code I’ve written to get the token:

     * Request code for auto Google Play Services error resolution.
    private static final int REQUEST_CODE_RESOLUTION = 89898;

     * User connected token.
    private String mToken;

     * Task for retrieving the token of the connected user
    private class GetTokenTask extends AsyncTask<String, Integer, String> {
        protected String doInBackground(String... userAccount) {
            String token = null;
            try {
                Log.d(TAG, "Retrieving token for [" + userAccount[0] + "]");
                token = GoogleAuthUtil.getToken(getActivity(), userAccount[0], Scopes.PROFILE, new Bundle());
            } catch (UserRecoverableAuthException e) {
                Log.w(TAG, "Error retrieving the token: " + e.getMessage());
                Log.d(TAG, "Trying to solve the problem...");
                startActivityForResult(e.getIntent(), REQUEST_CODE_RESOLUTION);
            } catch (IOException e) {
                Log.e(TAG, "Unrecoverable I/O exception: " + e.getMessage(), e);
            } catch (GoogleAuthException e) {
                Log.e(TAG, "Unrecoverable authentication exception: " + e.getMessage(), e);
            return token;

        protected void onPostExecute(String token) {
            if (token != null) {
                mToken = token;
                Log.d(TAG, "We have a token!: " + token);
            } else {
                Log.d(TAG, "Can't retrieve any token");

But always got the same error:

09-05 13:07:19.109  23577-25973/com.xxxxx E/Login﹕ Unrecoverable authentication exception: Unknown Unknown
            at Source)
            at com.xxxxx.views.LoginFragment$GetTokenTask.doInBackground(
            at com.xxxxx.views.LoginFragment$GetTokenTask.doInBackground(
            at android.os.AsyncTask$
            at android.os.AsyncTask$SerialExecutor$
            at java.util.concurrent.ThreadPoolExecutor.runWorker(
            at java.util.concurrent.ThreadPoolExecutor$

The error is all but descriptive. After browsing lot of pages I discovered the trick, the scope was wrong!. Well, the scope seems right, but it lacks a string header with the value ‘oauth2:’:

final String scope = "oauth2:" + Scopes.PROFILE;
token = GoogleAuthUtil.getToken(getActivity(), userAccount[0], scope, new Bundle());

After that, everything went right. Hope this helps!