20

In my android application, I am trying to get AccessToken from GoogleAuthUtil as below :

accessToken = GoogleAuthUtil.getToken(this, mPlusClient.getAccountName(), "oauth2:" + SCOPES);

But At this line I am gettting error as below :

E/GoogleAuthUtil(4696): Calling this from your main thread can lead to deadlock and/or ANRs E/GoogleAuthUtil(4696): java.lang.IllegalStateException: calling this from your main thread can lead to deadlock E/GoogleAuthUtil(4696): at com.google.android.gms.auth.GoogleAuthUtil.b(Unknown Source) E/GoogleAuthUtil(4696): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) E/GoogleAuthUtil(4696): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)

Any solution of this problem? Any help will be appreciated.

1
  • Hey @Ponting, this used to work for me, but only a few days ago it stopped working and now it throws this error com.google.android.gms.auth.GoogleAuthException: Unknown. Any idea why? Is there something wrong with my scopes? gist.github.com/lawloretienne/7351151 Commented Nov 7, 2013 at 17:03

4 Answers 4

46

Try it with an AsyncTask like this:

        AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String token = null;

                try {
                    token = GoogleAuthUtil.getToken(
                            MainActivity.this,
                            mGoogleApiClient.getAccountName(),
                            "oauth2:" + SCOPES);
                } catch (IOException transientEx) {
                    // Network or server error, try later
                    Log.e(TAG, transientEx.toString());
                } catch (UserRecoverableAuthException e) {
                    // Recover (with e.getIntent())
                    Log.e(TAG, e.toString());
                    Intent recover = e.getIntent();
                    startActivityForResult(recover, REQUEST_CODE_TOKEN_AUTH);
                } catch (GoogleAuthException authEx) {
                    // The call is not ever expected to succeed
                    // assuming you have already verified that 
                    // Google Play services is installed.
                    Log.e(TAG, authEx.toString());
                }

                return token;
            }

            @Override
            protected void onPostExecute(String token) {
                Log.i(TAG, "Access token retrieved:" + token);
            }

        };
        task.execute();

SCOPES is a space separated list of OAuth 2.0 scope strings. For example SCOPES could be defined as:

public static final String SCOPES = "https://www.googleapis.com/auth/plus.login "
    + "https://www.googleapis.com/auth/drive.file";

These represent the permissions that your app is requesting from the user. The scopes requested in this example are documented here:

5
  • what is the SCOPES here?
    – Rethinavel
    Commented Mar 3, 2014 at 8:22
  • Edited my answer to explain SCOPES.
    – Lee
    Commented Mar 6, 2014 at 21:04
  • if you dont need to access drive, you shuold remove 'developers.google.com/drive/android/files' from scopes Commented Apr 10, 2014 at 14:27
  • Is it also possible retrieve the access token expiration time using some Android Google API? Or only through a HTTPS request?
    – JP Ventura
    Commented Dec 4, 2014 at 16:51
  • @Lee Do you have any Clue about fetching the refresh token? Commented Jan 7, 2015 at 11:00
5

Use separate thread for your internet code. Error is telling that more time consuming process is running in the app and here that is internet. So use separate thread or Async task.

Check out this link NetworkOnMainThreadException

Hope it will help you.

0

E/GoogleAuthUtil(4696): java.lang.IllegalStateException: calling this from your main thread can lead to deadlock

Sounds like you need to do that on a separate thread, have you tried that?

Here you can find information about threads in Android.

1
  • I tried with to run above code under new thread : new Thread(new Runnable() { public void run() { } }.start(); ..But it gives error that GoogleAuthUtil.getToken can not run on runnable method.
    – Ponting
    Commented Jul 9, 2013 at 11:32
0
            Thread CrearEventoHilo = new Thread(){
                public void run(){
                    //do something that retrun "Calling this from your main thread can lead to deadlock"
                }
            };
            CrearEventoHilo.start();

CrearEventoHilo.interrupt();

public class Foo {
    MyThread mTh;
    void cantBeBothered() {
        mTh = new MyThread( /*...*/ );
        mTh.run();
        mTh.start();
    }
    void imFinishedNowWaitingForThread() {
        mTh.join();
    }
    void imOutKillingOffPendingThread()  {
        mTh.interrupt();
    }
//  .....
    private class MyThread extends Thread {
//      ...;
        MyThread( /*...*/) {
//          this... = ...;
        }
        public void run() {
            doSomething( /*this...*/ );
        }
    }
}
2
  • 2
    Your answer will be more valuable if you add an explanation on how it works. Commented Mar 27, 2017 at 11:41
  • Why the call to interrupt() ? Commented Nov 27, 2017 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.