我有一款安卓游戏,它使用游戏服务。Play Games服务似乎链接到特定的Google登录。如果玩家开始游戏,它会自动登录到游戏中。
现在我也想把firebase也加入到我的游戏中,例如方便聊天。
如何使用play gamesuser accountcreate/login转到firebase帐户?
有什么代币游戏可以让我直接进入火场吗?
我试图避免使用我自己的后端服务器,并避免让用户必须在我的游戏中登录两次,因为这是一个相当糟糕的用户体验。
我有什么选择?我对如何处理这个问题相当困惑。
--解决--
首先,我需要从firebase auth选项卡启用google登录。
然后我在基本活动中使用了以下代码:

private FirebaseAuth mAuth;

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
        .requestIdToken("<appidfromfirebase>.apps.googleusercontent.com")
        .build();


mGoogleApiClient = new GoogleApiClient.Builder(this)
    .enableAutoManage(this, this)
    .addConnectionCallbacks(this)
    .addApi(Games.API)
    .addScope(Games.SCOPE_GAMES)
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
    .build();


@Override
    public void onConnected(Bundle connectionHint) {
        if (mGoogleApiClient.hasConnectedApi(Games.API)) {
            Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient).setResultCallback(
                    new ResultCallback<GoogleSignInResult>() {
                        @Override
                        public void onResult(GoogleSignInResult googleSignInResult) {
                            GoogleSignInAccount acct = googleSignInResult.getSignInAccount();
                            if (acct == null) {
                                Logger.e("account was null: " + googleSignInResult.getStatus().getStatusMessage());
                                return;
                            }

                            AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(),null);

                            mAuth.signInWithCredential(credential)
                                    .addOnCompleteListener(
                                            new OnCompleteListener<AuthResult>() {
                                                @Override
                                                public void onComplete(@NonNull Task<AuthResult> task) {
                                                    // check task.isSuccessful()
                                                }
                                            });
                        }
                    }
            );
        }
    }

最佳答案

您需要使用用户的id令牌将google标识链接到Firebase Authentication
使用games配置构建google api客户端,请求id令牌。

GoogleSignInOptions options = new GoogleSignInOptions
            .Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
            .requestIdToken(firebaseClientId)
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Games.API)
            .addApi(Auth.GOOGLE_SIGN_IN_API, options)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

然后启动登录意图:
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);

当返回结果时,获取id令牌并将其传递给firebase.auth:
@Override
protected void onActivityResult(int requestCode, int responseCode,
                                Intent intent) {
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result =
                Auth.GoogleSignInApi.getSignInResultFromIntent(intent);
        if (result.isSuccess()) {
          AuthCredential credential = GoogleAuthProvider.getCredential(
                                          acct.getIdToken(), null);
          FirebaseAuth.getInstance().signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                }
            });
        }
    }
}

07-28 03:17
查看更多