即使成功登录,GoogleAPIClient也为空

时间:2016-10-24 15:03:07

标签: java android google-signin

我已在Android应用中成功添加了Google身份验证。我能够正确登录而不会出现任何错误。但是,当我尝试注销GoogleApiClient时,我给了null,因此我无法成功注销。我在这里尝试了很多答案,但没有任何对我有用。以下是我在MainActivity中输入的代码。

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            if(ApplicationPreferences.get().isFirstTimeUser()) {
                GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                        .requestScopes(new Scope(GmailScopes.GMAIL_READONLY))
                        .requestServerAuthCode(Constants.SERVER_CLIENT_ID, true)
                        .requestEmail()
                        .build();

                signInButton.setSize(SignInButton.SIZE_STANDARD);
                signInButton.setScopes(gso.getScopeArray());
                signInButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        switch (view.getId()) {
                            case R.id.sign_in_button:
                                signIn();
                                break;
                        }
                    }
                });
                signInButton.setVisibility(View.VISIBLE);

                // Build a GoogleApiClient with access to the Google Sign-In API and the
                // options specified by gso.
                mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
                        .enableAutoManage(MainActivity.this, MainActivity.this)
                        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                        .addConnectionCallbacks(MainActivity.this)
                        .build();
           mGoogleApiClient.connect();
            } else {
                loadMainActivity();
            }
        }
    }, 2000);

以下是Documentation指定的signOut方法,但我无法理解他们的陈述您必须在调用signOut之前确认已调用GoogleApiClient.onConnected。需要知道我的意思我在这里做错了。

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

                        ApplicationPreferences.get().clearAll();
                        Intent intent = getIntent();
                        finish();
                        startActivity(intent);

                    }
                });
    }

我的OnStart()

@Override
public void onStart() {
    super.onStart();

    OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
    if (opr.isDone()) {
        // If the user's cached credentials are valid, the OptionalPendingResult will be "done"
        // and the GoogleSignInResult will be available instantly.
        Log.d(TAG, "Got cached sign-in");
        GoogleSignInResult result = opr.get();
        handleSignInResult(result);
    } else {
        // If the user has not previously signed in on this device or the sign-in has expired,
        // this asynchronous branch will attempt to sign in the user silently.  Cross-device
        // single sign-on will occur in this branch.
        showProgressDialog();
        opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(GoogleSignInResult googleSignInResult) {
                hideProgressDialog();
                handleSignInResult(googleSignInResult);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

错误,如果你说没有调用回调,可能只是你可能没有先连接的标志。我想你已经实现了回调并且它们没有被调用?

在任何情况下,要“处理”错误情况,您可能需要检查GoogleApiClient是否已连接..如下所示:

if (mGoogleApiClient != null && mGoogleApiClient.isConnected())
    {
        Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {

                        ApplicationPreferences.get().clearAll();
                        Intent intent = getIntent();
                        finish();
                        startActivity(intent);

                    }
                });
    }
else{
   Log.w("SomeTag", "It looks like GoogleApiClient is not connected");
}

但我确实认为您需要检查是否有任何错误(例如,onConnectionFailed(ConnectionResult result)会被调用吗?您看到了什么错误?

希望这会有所帮助。