GoogleApiClient登陆失败

14

我想在我的Android游戏中使用谷歌游戏的回合制API。我用来连接GoogleApiClient的代码来自谷歌的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示例。这将创建一个带有以下消息的对话框:

登录失败,请检查您的网络连接并重试。

连接从未建立。
   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。从文档中可以看出:

当登录失败时,返回给调用Activity的结果代码。

尝试登录Games服务失败。例如,如果网络不稳定,或者用户的帐户已被禁用,或者无法获得同意,则可能会发生这种情况。

这让我感到困惑,因为在样本中,我没有遇到任何问题来使登录流程无法工作。但是,在我的游戏中,我从未收到选择Google帐户的提示,就会登录失败。

记录一下,我已经尝试了所有步骤https://developers.google.com/games/services/android/troubleshooting ,但仍然失败。

如何解决此错误以进行登录?


3
你的日志也可能有所帮助。 - MohammedAlSafwan
你尝试过访问 https://dev59.com/qoTca4cB1Zd3GeqPB_s9?rq=1 吗?你能否发布一个 MCVE - serv-inc
2个回答

3
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();
}

更多参考信息,请查看链接

该链接涉及到与Android游戏服务相关的IT技术。

在某个时候,我已经解决了我的问题,但是时间太久了,我实际上忘记了我做了什么。为了选择一个被接受的答案,我接受了这个答案。谢谢 :) - BLUC

0
尝试使用 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();
    }

以上问题是针对Google游戏API而不是位置监听器的...! - user6602265

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接