Google Play登录失败Api例外4

时间:2018-03-14 08:27:19

标签: google-play-services

我试图整合谷歌登录并从另一个项目中获取代码,但是对于新的应用程序,它通过登录但允许的对话框不显示,而是我用Api登录失败例外4。

以下是所有适用的代码:

//Create the client used to sign in to Google services
        mGoogleSignInClient = GoogleSignIn.getClient(this,
                new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
                        .requestServerAuthCode(getString(R.string.client_id))
                        .requestEmail()
                        .build());
private void signInSilently(){
        Log.d(TAG, "signInSilently()");

        mGoogleSignInClient.silentSignIn().addOnCompleteListener(this,
                new OnCompleteListener<GoogleSignInAccount>() {
                    @Override
                    public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                        if (task.isSuccessful()) {
                            Log.d(TAG, "signInSilently(): success");

                            onConnected(task.getResult());
                        } else {
                            Log.d(TAG, "signInSilently(): failure", task.getException());
                            onDisconnected();
                        }
                    }
                });
    }

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

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task =
                    GoogleSignIn.getSignedInAccountFromIntent(data);

            try {
                GoogleSignInAccount account = task.getResult(ApiException.class);
                onConnected(account);
            } catch (ApiException apiException) {
                String message = apiException.getMessage();
                if (message == null || message.isEmpty()) {
                    message = getString(R.string.signin_other_error);
                }

                onDisconnected();

                new AlertDialog.Builder(this)
                        .setMessage(message)
                        .setNeutralButton(android.R.string.ok, null)
                        .show();
            }
        }
    }
private void onDisconnected() {
        Log.d(TAG, "onDisconnected()");

        mPlayersClient = null;
        // Show sign-in button on main menu
        if(game.user != null) {
            game.user.setDisplayName(null);
            game.user.setPlayerID(null);
        }
    }

    private void onConnected(final GoogleSignInAccount googleSignInAccount) {
        Log.d(TAG, "onConnected(): connected to Google APIs");

        GamesClient gamesClient = Games.getGamesClient(AndroidLauncher.this, googleSignInAccount);
        View view = ((AndroidGraphics) Gdx.graphics).getView();
        gamesClient.setViewForPopups(view);
        gamesClient.setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL);

        mPlayersClient = Games.getPlayersClient(this, googleSignInAccount);

        // Set the greeting appropriately on main menu
        mPlayersClient.getCurrentPlayer()
                .addOnCompleteListener(new OnCompleteListener<Player>() {
                    @Override
                    public void onComplete(@NonNull Task<Player> task) {
                        String displayName;
                        if (task.isSuccessful()) {
                            user = new User();
                            user.setDisplayName(task.getResult().getDisplayName());
                            user.setPlayerID(task.getResult().getPlayerId());
                            game.user = user;
                            firebaseAuthWithPlayGames(googleSignInAccount);
                        } else {
                            Exception e = task.getException();
                            handleException(e, getString(R.string.players_exception));
                            displayName = "???";
                        }
                    }
                });
    }

    private void handleException(Exception e, String details) {
        int status = 0;

        if (e instanceof ApiException) {
            ApiException apiException = (ApiException) e;
            status = apiException.getStatusCode();
        }

        String message = getString(R.string.status_exception_error, details, status, e);

        new AlertDialog.Builder(AndroidLauncher.this)
                .setMessage(message)
                .setNeutralButton(android.R.string.ok, null)
                .show();
    }

0 个答案:

没有答案