GoogleApiClient 登录失败

GoogleApiClient Sign In Fails

我正在尝试将 Google 回合制游戏 Api 用于我的 Android 游戏。我用来连接 GoogleApiClient 的代码来自 Google 的 Api 示例或文档。

在我实施 onConnectionFailed 的过程中,我尝试了两种不同的方法:

    if (signInClicked || autoStartSignInFlow) {
        autoStartSignInFlow = false;
        signInClicked = false;
        resolvingConnectionFailure = true;

         // Attempt to resolve the connection failure using BaseGameUtils.
         // The R.string.signin_other_error value should reference a generic
         // error string in your strings.xml file, such as "There was
         // an issue with sign-in, please try again later."
        if (!BaseGameUtils.resolveConnectionFailure(this,
                apiClient, connectionResult,
                RC_SIGN_IN, R.string.signin_other_error)) {
            resolvingConnectionFailure = false;
        }
    }

上面的第一种方法来自 TBMP Skeleton 示例。这会导致使用消息

创建一个对话框

Failed to sign in. Please check your network connection and try again.

并且永远不会建立连接。

   if (connectionResult.hasResolution()) {
        // https://developers.google.com/android/guides/api-client under 'Handle connection
        // failures'. I don't know if this is solving the problem but it doesn't lead to
        // 'please check your network connection' message.
        try {
            if(LoggerConfig.ON) {
                Log.e(TAG, "onConnectionFailure, attempting to startResolutionForResult.");
            }
            resolvingConnectionFailure = true;
            connectionResult.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
        } catch (IntentSender.SendIntentException e) {
            // There was an error with the resolution intent. Try again.
            if(LoggerConfig.ON) {
                Log.e(TAG, "onConnectionFailure, there was an error with resolution intent");
            }
            apiClient.connect();
        }
    }

在第二种方法中,它最终调用 startResolutionForResult,将 RESULT_SIGN_IN_FAILED 传递给 onActivityResult。来自文档

Result code sent back to the calling Activity when signing in fails.

The attempt to sign in to the Games service failed. For example, this might happen if the network is flaky, or the user's account has been disabled, or consent could not be obtained.

这让我感到困惑,因为我可以毫无问题地让登录流程在示例中工作。但是,在我的游戏中,在登录失败之前,我从未收到 select 一个 Google 帐户的提示。

郑重声明,我已经尝试了此处的所有步骤https://developers.google.com/games/services/android/troubleshooting,但仍然失败。

如何解决此错误才能登录?

尝试private GoogleApiClient mGoogleApiClient;

 mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

onStart()方法中调用mGoogleApiClient.connect();

@Override
    public void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }
private GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create the Google Api Client with access to the Play Games services
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            // add other APIs and scopes here as needed
            .build();

    // ...
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}

如需进一步参考,请查看 link link