简体   繁体   中英

Google SignIn on Android - Log out does not work since not logged in

I have integrated Google SignIn via this guide ( https://developers.google.com/identity/sign-in/android/start-integrating ).

I've done setup like this:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestIdToken(AppActivity.this.getResources().getString(R.string.server_client_id))
            .build();

GoogleSignIn.googleApiClient = new GoogleApiClient.Builder(this)
            .addOnConnectionFailedListener(this)
            .addConnectionCallbacks(this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

Sign in works with the following code and works perfectly fine:

public static void loginGoogleSDK()
{
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
    GameApplication.getActivity().startActivityForResult(signInIntent, RC_SIGN_IN);
}

The SignIn-Overlay appears, I choose an account and log in. Everything works fine.

Then I try to call logout with the following code after this guide ( https://developers.google.com/identity/sign-in/android/disconnect ), but I always the the error message: Cannot log out, since not logged in .

Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(
        new ResultCallback<Status>() {
            @Override
            public void onResult(Status status) {
                System.out.println("Google SDK Sign Out Access Status:" + status);
            }
        });

The weird part is, that if I call login again, it is automatically successful and I can not choose an account again. Hence, the login is still active and I can not log out.

Per the enableAutoManage documentation , including it:

Enables automatic lifecycle management in a support library FragmentActivity that connects the client in onStart() and disconnects it in onStop() .

It handles user recoverable errors appropriately and calls onConnectionFailed(ConnectionResult) on the unresolvedConnectionFailedListener if the ConnectionResult has no resolution. This eliminates most of the boiler plate associated with using GoogleApiClient .

By not including enableAutoManage() like the Add Sign In guide does, your GoogleApiClient never actually connects, causing the error you are experiencing.

If you don't want to use enableAutoManage() , you can follow the instructions to manually manage connections including providing an implementation for ConnectionCallbacks and OnConnectionFailedListener .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM