Google 在 Android 上登录 - 由于未登录,因此无法注销

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

我已经通过本指南 (https://developers.google.com/identity/sign-in/android/start-integrating) 集成了 Google 登录。

我是这样设置的:

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();

使用以下代码登录并完美运行:

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

SignIn-Overlay 出现,我选择一个帐户并登录。一切正常。

然后我尝试在本指南之后使用以下代码调用注销 (https://developers.google.com/identity/sign-in/android/disconnect),但我总是收到错误消息:无法注销,因为未登录.

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

奇怪的是,如果我再次调用登录,它会自动成功并且我不能再次选择帐户。因此,登录仍然有效,我无法退出。

根据 enableAutoManage documentation,包括它:

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.

由于不像 Add Sign In guide 那样包含 enableAutoManage(),您的 GoogleApiClient 从未真正连接,导致您遇到错误。

如果您不想使用 enableAutoManage(),您可以按照 manually manage connections 的说明进行操作,包括为 ConnectionCallbacksOnConnectionFailedListener 提供实现。