3

我正在像这样设置 GoogleSignInOptions 和 Google Api Client

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.server_client_ID))
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .addApi(Plus.API)
            .build();

和我的谷歌网络应用程序客户端 ID 是这样的:

 1020847812450-xxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com

但总是在 onActivityResult

    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInResult(result);
    }

返回错误

我在哪里做错了:S

onStart 部分

    mGoogleApiClient.connect();
    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.

        opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(GoogleSignInResult googleSignInResult) {

                handleSignInResult(googleSignInResult);
            }
        });
    }

onStop 部分

protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

onHandleSignInResult

private void handleSignInResult(GoogleSignInResult result) {
    Log.e(TAG, "handleSignInResult:" + result.isSuccess());
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        final GoogleSignInAccount acct = result.getSignInAccount();
        Log.e(TAG, acct.getDisplayName());

    }
}
4

4 回答 4

2

我也面临同样的问题。首先删除当前的 OAuth 客户端 ID,然后再创建一个 OAuth 客户端 ID。它对我有用。

于 2015-12-16T14:02:16.493 回答
0

您收到错误 12501 吗?我也遇到了这个问题,因为我使用debug.keystore的是 SDK 附带的(由于某种我不知道的原因,它不起作用)。我自己创建了一个新的,从中获取 SHA-1 哈希,输入 Google API 控制台,然后它就可以工作了。

确保使用新的 keystore 为调试和发布版本设置签名配置。

于 2017-04-19T13:06:49.770 回答
0

按照所有步骤!

发布 APK 和调试 APK 具有不同的 SHA1 和不同的谷歌服务 API 密钥。两者都必须在 Firebase 控制台 -> 项目设置中添加。然后从此处下载 google-services.json,将其添加到项目并使用“构建签名的 APK”选项使用发布密钥库重新编译。那应该工作

并仔细阅读...

https://developer.android.com/studio/publish/app-signing

于 2018-09-12T15:15:25.427 回答
-1

我相信您需要client.connect();根据文档示例调用:

GoogleApiClient client = new GoogleApiClient.Builder(this)
         .addApi(Plus.API)
         .addScope(Plus.SCOPE_PLUS_LOGIN)
         .setAccountName("users.account.name@gmail.com")
         .build();
 client.connect();

还是您的问题中缺少它并且您connect在代码中的其他地方调用了它?

于 2015-12-14T10:01:47.233 回答