如何 link Firebase 帐户到 Google Play 游戏帐户

How to link Firebase account to Google Play Games account

我有一款使用 Play 游戏服务的 Android 游戏。 Play 游戏服务似乎链接到特定的 Google 登录名。如果玩家开始游戏,它会自动登录到 Play Games。

现在我也希望将 Firebase 整合到我的游戏中,例如方便聊天。

如何使用 Play 游戏 user accountcreate/login 到 Firebase 帐户?

Play Games 是否提供了某种可以传递给 Firebase 的令牌?

我试图避免必须使用我自己的后端服务器,并避免让用户必须在我的游戏中登录两次,因为那是相当糟糕的用户体验。

我有哪些选择?我对如何处理这个问题感到很困惑。

-- 已解决 --

首先,我需要从 Firebase 身份验证选项卡启用 Google 登录。 然后我在我的基地 activity:

中使用了这段代码
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()
                                                }
                                            });
                        }
                    }
            );
        }
    }

您在 Android 设备上登录的 Google 帐户通常是 linked with your GPGS profile

现在要回答您的问题,请按照有关如何将 Firebase 与 GPGS 结合使用的 Integrate With Your Play Games Services Project 指南进行操作。

When you add Firebase to your Play Game Services project in the Play Developer console, you can:

Get access to Firebase Analytics, a free app measurement solution that provides insight on app usage and user engagement. View your Games events in the Firebase Analytics by logging Play Games events to Firebase.

When you add Firebase to your Play Games Services project, you’re also linking your Google Play Account to your Firebase project.

Add Firebase to your Play Games Services project

In the Play Developer Console:

Click Add Firebase on the Game details page of your app Review the policy confirmation, then click Confirm Now Firebase is added to your Play Games Project and your Google Play account is linked to your Firebase project. Next, you will need to add the Firebase SDK to your app's code.

Add the Firebase SDK to your app

To get started, add Firebase Analytics to your app.

Once you have added the Firebase Analytics SDK to your app, you can begin logging Play Games events.

您需要使用 id 令牌让用户 link 将 Google 身份转换为 Firebase Authentication

使用游戏配置构建 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 token,传给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());

                }
            });
        }
    }
}