java - Android 上的 Google 登录 - 由于未登录而无法注销

标签 java android authentication google-signin

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

我已经完成了这样的设置:

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

出现登录界面,我选择一个帐户并登录。一切正常。

然后我尝试在本指南之后使用以下代码调用注销( 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);
            }
        });

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

最佳答案

根据 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.

不包含enableAutoManage(),如 Add Sign In guide确实如此,您的 GoogleApiClient 从未真正连接,导致您遇到错误。

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

关于java - Android 上的 Google 登录 - 由于未登录而无法注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39195908/

相关文章:

java - Eclipse 喜欢它,javac 讨厌它,它是一个带有接口(interface)的枚举

java - Android/Java线程同步: while(true){} causing blocking

python - Turbogears 通过函数调用改变用户

php - 我正在android中制作一个注册页面

java - JSONException : Value of type java. lang.String 无法转换为 JSONArray

java - 迷你桶尺寸

android - 我如何格式化解析并传递给 editText 的数字?

java - 按钮未更新其文本

android - 使用 AWS 的典型应用程序用户身份验证

java - 执行 bash 脚本并从 java 传递带空格的参数