0

I am trying to retrieve the Google Plus access token for a signed in user in my Android application. I am doing this using the following code, which follows suggestions made here, here and here:

private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        int REQUEST_CODE_TOKEN_AUTH = 466453; // Any number, not really using it anyway.
        String accountName = params[0];
        String scopes = "oauth2:" + Plus.SCOPE_PLUS_LOGIN;
        String token = null;
        try {
            token = GoogleAuthUtil.getToken(getActivity(), accountName, scopes);
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        } catch (UserRecoverableAuthException e) {
            startActivityForResult(e.getIntent(), REQUEST_CODE_TOKEN_AUTH);
        } catch (GoogleAuthException e) {
            Log.e(TAG, e.getMessage());
        }

        return token;
    }
}

I am calling it like this:

@Override
public void onConnected(Bundle connectionHint) {
    mSignInClicked = false;

    Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

    try {
        String accessToken = (new RetrieveTokenTask()).execute(Plus.AccountApi.getAccountName(mGoogleApiClient)).get();
    } catch (Exception ex) {
        Log.e(TAG, ex.getMessage());
    }
}

I have the following permissions in my AndroidManifest (among others):

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

The problem is that when calling GoogleAuthUtil.getToken(), there is no progress being made and it will just freeze there. How can I fix this?

Community
  • 1
  • 1
Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114

1 Answers1

1

The problem is that when calling GoogleAuthUtil.getToken(), there is no progress being made and it will just freeze there.

Because you are calling get() method AsyncTask which make Main UI Thread wait until doInBackground computation not completed

How can I fix this?

Execute AsyncTask by calling execute method and use onPostExecute to get accessToken

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • This is just a quick try to ensure that the token retrieval works fine. I will update that after I fix this, but the real question is why does the getToken() method not make any progress? – Vlad Schnakovszki Jan 29 '15 at 11:12
  • @VladSchnakovszki: First remove `get()` method then check it – ρяσѕρєя K Jan 29 '15 at 11:13
  • @VladSchnakovszki: and also make sure INTERNET permission is also available – ρяσѕρєя K Jan 29 '15 at 11:15
  • Ok, that fixed it .. Wasn't expecting that. If you could please detail your answer a bit on why this is happening I will award it as the accepted answer. Thanks! – Vlad Schnakovszki Jan 29 '15 at 11:39
  • @VladSchnakovszki: my answer is already in detail. application is freezing because when we call get method of `AsyncTask` then system block control until doInBackground computation not completed but with out `get` method `doInBackground` run in background thread without blocking execution of main ui Thread and when `doInBackground` method execution completed system call `onPostExecute ` method to inform main ui thread about `doInBackground` method execution is completed. for more details see [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html) – ρяσѕρєя K Jan 29 '15 at 11:44