【问题标题】:GoogleApiClient: Can't manually connect and perform signOut afterwardsGoogleApiClient:之后无法手动连接并执行注销
【发布时间】:2017-06-17 22:31:33
【问题描述】:

我想手动处理GoogleApiClient 上的connect()disconnect() 操作。我正在尝试:

  • 建立一个新的GoogleApiClient(没有enableAutoManage
  • 致电connect()
  • onConnected()被调用时执行signOut
  • signOut 结束后致电disconnect()

这是一个例子:

fun signOut(googleApiClient: GoogleApiClient, resultCallback: (Status) -> Unit) {
    Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(resultCallback)
}

fun test() {
    val googleApiClient = GoogleApiClient.Builder(activity)
        .addApi(Auth.GOOGLE_SIGN_IN_API, buildGoogleSignInOptions(googleAuthId))
        .build()
    googleApiClient.registerConnectionCallbacks(object : ConnectionCallbacks {
        override fun onConnected(connectionHint: Bundle?) {
            signOut { status ->
                //TODO something with status
                googleApiClient.disconnect()
            }
        }

        override fun onConnectionSuspended(cause: Int) {
            //nop
        }
    })
    googleApiClient.registerConnectionFailedListener {
        //TODO handle failure
    }
    googleApiClient.connect()
}

但是,当onConnected() 被调用时,signOut 调用失败并显示

IllegalStateException: GoogleApiClient is not connected yet 

我做错了什么还是图书馆的错误?

【问题讨论】:

  • 这个SO thread的错误似乎是一样的并且已经解决了。
  • @noogui 谢谢!在onCreate() 中移动客户端创建听起来更像是一种解决方法而不是解决方案,但我想我无能为力了。正如在这个帖子中所说,没有办法向谷歌报告这个错误......

标签: android google-play-services kotlin google-signin


【解决方案1】:

我们需要在 onCreate() 中创建 googleAPIClient 并在同一个活动中注销它。

例如: 在 onCreate() 中

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(this.getString(R.string.default_web_client_id))
            .requestEmail()
            .requestProfile()
            .build();


    mGoogleApiClient = new GoogleApiClient.Builder(activity)
            .enableAutoManage(activity, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                }
            })
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(@Nullable Bundle bundle) {
                }

                @Override
                public void onConnectionSuspended(int i) {

                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

然后退出:

    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {

                    if (mListener != null) {
                        mListener.onSigout("");
                    }
                }
            });

如果我们使用 ApplicationContext 创建 mGoogleAPIClient,我们将无法正确退出 Google 帐户。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2019-02-12
    相关资源
    最近更新 更多