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> {
        @Override
        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;
        }

        @Override
        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
    com.google.android.gms.auth.GoogleAuthException: Unknown
            at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
            at com.xxxxx.views.LoginFragment$GetTokenTask.doInBackground(LoginFragment.java:232)
            at com.xxxxx.views.LoginFragment$GetTokenTask.doInBackground(LoginFragment.java:225)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

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!

Android: Could not access the Package Manager. Is the system running?

I work on several little Android apps which I update sometimes. One or two years ago I was using Eclipse for that, and it worked and works well, but several months ago I changed to Android Studio. This new tool is based on IntelliJ and it’s very comfortable, but it’s in an early stage and sometimes does strange things.

After last update to 0.3.2 version I experimented an error I never had before, or at least I don’t recall to have it. Trying to running the app in the phone emulator I got this error when Android Studio was installing the app through the package manager:

DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.elpaso.android.gpro.ps"
Could not access the Package Manager. Is the system running?

Obviously the system was running, and all of this was working before the update, as usual, so I was a bit concerned because when this kind of strange things happen, it means you’re going to waste time searching for a solution. Well, finally I found the solution, it was easier than I thought… simply unlock the emulator sliding the lock icon. After unlocking it I ran the process again and the app was installed right.

P.S.: Always use a real phone if you can when working with Android apps, it’s lot of times faster.