Android Google+ integration - repeated UserRecoverableAuthException Android Google+ integration - repeated UserRecoverableAuthException android android

Android Google+ integration - repeated UserRecoverableAuthException


I've had this issue for a while and came up with a proper solution.

String token = GoogleAuthUtil.getToken(this, accountName, scopeString, appActivities);

This line will either return the one time token or will trigger the UserRecoverableAuthException.On the Google Plus Sign In guide, it says to open the proper recovery activity.

startActivityForResult(e.getIntent(), RECOVERABLE_REQUEST_CODE);

When the activity returns with the result, it will come back with few extras in the intent and that is where the new token resides :

@Overrideprotected void onActivityResult(int requestCode, int responseCode, Intent intent) {    if (requestCode == RECOVERABLE_REQUEST_CODE && responseCode == RESULT_OK) {        Bundle extra = intent.getExtras();        String oneTimeToken = extra.getString("authtoken");    }}

With the new oneTimeToken given from the extra, you can submit to the server to connect properly.

I hope this helps!


Its too late to reply but it may help to people having same concern in future.

They have mentioned in the tutorial that it will always throw UserRecoverableAuthException when you invoke GoogleAuthUtil.getToken() for the first time. Second time it will succeed.

catch (UserRecoverableAuthException e) {  // Requesting an authorization code will always throw  // UserRecoverableAuthException on the first call to GoogleAuthUtil.getToken  // because the user must consent to offline access to their data.  After  // consent is granted control is returned to your activity in onActivityResult  // and the second call to GoogleAuthUtil.getToken will succeed.  startActivityForResult(e.getIntent(), AUTH_CODE_REQUEST_CODE);  return;}

i used below code to get access code from google.

execute this new GetAuthTokenFromGoogle().execute(); once from public void onConnected(Bundle connectionHint) and once from protected void onActivityResult(int requestCode, int responseCode, Intent intent)

private class GetAuthTokenFromGoogle extends AsyncTask<Void, Integer, Void>{        @Override          protected void onPreExecute()          {          }        @Override        protected Void doInBackground(Void... params) {            // TODO Auto-generated method stub            try {                accessCode = GoogleAuthUtil.getToken(mContext, Plus.AccountApi.getAccountName(mGoogleApiClient), SCOPE);                new ValidateTokenWithPhoneOmega().execute();                Log.d("Token  -- ", accessCode);            } catch (IOException transientEx) {                // network or server error, the call is expected to succeed if you try again later.                // Don't attempt to call again immediately - the request is likely to                // fail, you'll hit quotas or back-off.                return null;            } catch (UserRecoverableAuthException e) {                // Recover                startActivityForResult(e.getIntent(), RC_ACCESS_CODE);                e.printStackTrace();            } catch (GoogleAuthException authEx) {                // Failure. The call is not expected to ever succeed so it should not be                // retried.                authEx.printStackTrace();                return null;            } catch (Exception e) {                throw new RuntimeException(e);            }            return null;          }        @Override          protected void onPostExecute(Void result)          {         }    }


I have got around this issue by using a web based login. I open a url like this

String url = "https://accounts.google.com/o/oauth2/auth?scope=" + Scopes.PLUS_LOGIN + "&client_id=" + webLoginClientId + "&response_type=code&access_type=offline&approval_prompt=force&redirect_uri=" + redirect;

The redirect url then handles the response and returns to my app.

In terms of my findings on using the Google Play Services, I've found:

HTC One is 3.1.59 (736673-30) - not workingGalaxy Note is 3.1.59 (736673-36) - not workingNexus S is 3.1.59 (736673-34) - works

And I'd like to be involved in the chat that is occurring, however I don't have a high enough reputation to do so.