Google登录 - GoogleSignInAccount返回null

时间:2018-05-03 11:06:16

标签: android google-api

我试图在我的应用中集成谷歌标志,但它一直失败,GoogleSignInAccount返回一个空对象并显示已取消。我不知道什么是错的。是这个还是依赖?这是我在onCreate中的代码

    GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions
            .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestProfile()
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
            .build();


    final SignInButton googleLoginButton = (SignInButton) findViewById(R.id.googleLoginButton);
    googleLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View view) {
            googleSignIn();
        }
    });

这些是其他功能

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    // do nothing
}

private void googleSignIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInResult(result);
    } else {
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

private void handleSignInResult(GoogleSignInResult result) {
    if (result.isSuccess()) {
        GoogleSignInAccount acct = result.getSignInAccount();
        if (acct != null) {
            Toast.makeText(MainActivity.this, "Welcome " + acct.getDisplayName(),
                    Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(MainActivity.this, "Cancelled!",
                Toast.LENGTH_SHORT).show();
    }
}

0 个答案:

没有答案