【问题标题】:Google Play Games API returns SIGN_IN_REQUIREDGoogle Play 游戏 API 返回 SIGN_IN_REQUIRED
【发布时间】:2018-12-14 02:11:21
【问题描述】:

我正在尝试在我的 Android 应用中实现自动玩家登录到 Google Play 游戏。首先,如here所述,我尝试静默登录:

@Override
protected void onResume() {
    super.onResume();
    signInSilently();
}

private void signInSilently() {
    mGoogleSignInClient.silentSignIn().addOnCompleteListener(this, task -> {
        if (task.isSuccessful())
            //everything ok
        else {
            final ApiException exception = (ApiException) task.getException();
            if (BuildConfig.DEBUG)
                Log.d(TAG, "Silent Sign In failure: ", exception);
            if (exception.getStatusCode() == CommonStatusCodes.SIGN_IN_REQUIRED)
                startSignInIntent();
        }
    });

每次我遇到代码 4 (CommonStatusCodes.SIGN_IN_REQUIRED) 的异常时。所以在这种情况下,我尝试使用 ui 登录:

private void startSignInIntent() {
    startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN);
}

@Override
protected void onActivityResult(int request, int response, Intent data) {
    super.onActivityResult(request, response, data);
    if (request == RC_SIGN_IN) {
        final GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            // everything is ok, get account from result
        } else if (result.getStatus().hasResolution()) {
            resolveManually(result.getStatus());
        } else {
            String message = result.getStatus().getStatusMessage();
            if (BuildConfig.DEBUG)
                Log.d(TAG, "status code" + result.getStatus().getStatusCode());
            if (message == null || message.isEmpty()) {
                message = "other error";
            }
            new AlertDialog.Builder(this).setMessage(message)
                    .setNeutralButton(android.R.string.ok, null).show();
        }
    }
}

每次我收到带有其他错误的消息时都在这里!状态代码再次为 4 (CommonStatusCodes.SIGN_IN_REQUIRED)。当我尝试使用 Intent 登录时如何获取此代码?因此,我的应用程序处于无限循环中,因为每次我的活动在收到结果后加载时都会调用 onResume,并且每次状态代码都是 CommonStatusCodes.SIGN_IN_REQUIRED。那么,问题出在哪里? 在 Google samples 中没有信息如何处理自动登录,只有手动登录按钮。但谷歌recommends 使用自动登录。请帮助任何人了解这里出了什么问题。

【问题讨论】:

    标签: java android google-signin google-play-games


    【解决方案1】:

    您不能从您的 onResume 方法启动登录屏幕。这是一个静默登录,如果用户想要它(通过点击一个按钮)就可以工作。这就是示例仅以这种方式显示的原因。

    【讨论】:

    • 我没有从 onResume 开始登录。 mGoogleSignInClient.silentSignIn() 尝试在没有用户交互的情况下登录,因此用户看不到任何内容。此无提示登录代码为 recommended by Google
    • 我在你的代码中看到了这个流程:onResume -> signInSilently() -> if signInRequired: startSignInIntent() - 所以你从 onResume 调用非静默方法,这是不允许的。跨度>
    【解决方案2】:

    我的应用程序的调试版本的 OAuth 2.0 客户端 ID 错误!不知道为什么这种情况会有SIGN_IN_REQUIRED状态码,真的很混乱!

    【讨论】:

    • 谢谢,这很令人困惑且不明显。
    猜你喜欢
    • 1970-01-01
    • 2018-12-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 2015-10-03
    • 2016-02-04
    • 2014-05-09
    相关资源
    最近更新 更多