gpt4 book ai didi

android - 如何将 Firebase 帐户关联到 Google Play 游戏帐户

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:58 26 4
gpt4 key购买 nike

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

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

如何使用 Play 游戏用户帐户创建/登录Firebase 帐户?

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

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

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

-- 已解决 --

首先,我需要从 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()
}
});
}
}
);
}
}

最佳答案

您需要使用用户的 id token 将 Google 身份链接到 Firebase Authentication .

使用游戏配置构建 Google API 客户端,请求 ID token 。

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

然后启动 SignIn Intent:

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

}
});
}
}
}

关于android - 如何将 Firebase 帐户关联到 Google Play 游戏帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43138946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com