xorgate
xorgate

Reputation: 2264

How to link Firebase account to Google Play Games account

I have an Android game that uses Play Games Services. Play Games Services appear to be linked to a specific Google Login. If a player starts the game, it is automatically logged into Play Games.

Now I wish to incorporate Firebase into my game as well, for example to facilitate chat.

How can I use the Play Games user account to create/login to a Firebase account?

Is there some sort of token Play Games gives me I can just pass onto Firebase?

I am trying to avoid having to use my own backend server, and avoid having the user having to sign in twice in my game, since that is a rather poor user experience.

What are my options? I'm rather stumped on how to approach this.

-- SOLVED --

First of all I needed to enable Google Signin from the Firebase auth tab. Then I used this code in my base 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()
                                                }
                                            });
                        }
                    }
            );
        }
    }

Upvotes: 2

Views: 1965

Answers (2)

Clayton Wilkinson
Clayton Wilkinson

Reputation: 4572

You need to use the id token for the user to link the Google identity to the Firebase Authentication.

Build the Google API client with the games config, requesting an 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();

Then launch the SignIn Intent:

    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);

And when the result is returned, get the id token and pass it to 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());

                }
            });
        }
    }
}

Upvotes: 2

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17631

Your logged-in Google account in your Android device is usually the one linked with your GPGS profile.

Now to answer your question, follow the Integrate With Your Play Games Services Project guide on how to use Firebase with GPGS.

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.

Upvotes: 0

Related Questions