Android Google+ integration - repeated UserRecoverableAuthException

前端 未结 8 1742
独厮守ぢ
独厮守ぢ 2020-11-30 00:55

We have contacted Google about this and we are on chat

The issue seems to be fixed for devices except Samsung phones.

I\'m

8条回答
  •  长情又很酷
    2020-11-30 01:24

    I had a similar problem where an apparent auth loop kept creating {read: spamming} these "Signing In..." and Permission request dialogs while also giving out the discussed exception repeatedly.

    The problem appears in some slightly-modified example code that I (and other like me, I suspect) "cargo-culted" from AndroidHive. The solution that worked for me was ensuring that only one background token-retrieval task runs at the background at any given time.

    To make my code easier to follow, here's the auth flow in my app (that is almost identical to the example code on AndoidHive): Activity -> onConnected(...) -> getProfileInformation() -> getOneTimeToken().

    Here's where getOneTimeToken() is called:

    private void getProfileInformation() {
        try {
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                Person currentPerson = Plus.PeopleApi
                        .getCurrentPerson(mGoogleApiClient);
                String personName = currentPerson.getDisplayName();
                String personPhotoUrl = currentPerson.getImage().getUrl();
                String personGooglePlusProfile = currentPerson.getUrl();
                String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
                getOneTimeToken(); // <-------
                ...
    

    Here's my getOneTimeToken():

    private void getOneTimeToken(){
        if (task==null){
        task = new AsyncTask() {
            @Override
            protected String doInBackground(Void... params) {
                LogHelper.log('d',LOGTAG, "Executing background task....");
                Bundle appActivities = new Bundle();
                appActivities.putString(
                             GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
                             ACTIVITIES_LOGIN);
                String scopes = "oauth2:server" + 
                                ":client_id:" + SERVER_CLIENT_ID + 
                                ":api_scope:" + SCOPES_LOGIN;
                String token = null;
                try {
                    token = GoogleAuthUtil.getToken(
                            ActivityPlus.this,
                            Plus.AccountApi.getAccountName(mGoogleApiClient),
                            scopes,
                            appActivities
                    );
                } catch (IOException transientEx) {
                    /* Original comment removed*/
                    LogHelper.log('e',LOGTAG, transientEx.toString());
                } catch (UserRecoverableAuthException e) {
                    /* Original comment removed*/
                    LogHelper.log('e',LOGTAG, e.toString());
                    startActivityForResult(e.getIntent(), AUTH_CODE_REQUEST);
                } catch (GoogleAuthException authEx) {
                    /* Original comment removed*/
                    LogHelper.log('e',LOGTAG, authEx.toString());
                } catch (IllegalStateException stateEx){
                    LogHelper.log('e',LOGTAG, stateEx.toString());
                }
                LogHelper.log('d',LOGTAG, "Background task finishing....");
                return token;
            }
    
            @Override
            protected void onPostExecute(String token) {
                LogHelper.log('i',LOGTAG, "Access token retrieved: " + token);
            }
    
        };
        }
        LogHelper.log('d',LOGTAG, "Task setup successful.");
        if(task.getStatus() != AsyncTask.Status.RUNNING){
            task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR); //double safety!
        } else
            LogHelper.log('d',LOGTAG, 
                           "Attempted to restart task while it is running!");
    }
    

    Please note that I have a {probably redundant} double-safety against the task executing multiple times:

    1. if(task .getStatus() != AsyncTask.Status.RUNNING){...} - ensures that the task isn't running before attempting to execute it.
    2. task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);- makes sure that copies of this task are "synchronized" (i.e. a queue is in place such that only one task of this type can executed at a given time).

    P.S. Minor clarification: LogHelper.log('e',...) is equivalent to Log.e(...) etc.

提交回复
热议问题