Setting up PlayFab authentication using Google Play games sign-in in Unity

Tutorial video

Note

The audio portion of the following video is currently unavailable. A new recording is pending, and will be available in the immediate future.

HowTo: Use Google SignIn (GPG) + PlayFab (Unity)

This tutorial illustrates the steps to successfully add Google Play Games sign-in to your PlayFab games, built with the Unity SDK. Actually, this should be a fairly simple procedure, once you know all the steps.

However, finding all the required information can be tricky. So we will try to centralize all the steps you need to get this feature working.

You can also download the sample for this tutorial here.

Prerequisites

Getting started

First, we will start by setting up some basic things in Google Play. There are a few good guides on how to do this, so we are not going to reinvent the wheel.

  1. Create an Application - If you have not done so already, you will need to create an application in the Google Play Developer Console.
  2. Build your game APK, and upload it as an Alpha Build.
    • Select Manage Releases.
    • Select Manage Alpha.
    • Select Upload APK.
  3. Setting Up Google Play Games Services - Watch the YouTube video, as the link here is dated, and does not reflect the latest UI of Google Play Games services.
  4. Download the Google Play Games SDK (if you have not done so already) and install it.
  5. Install the PlayFab Unity SDK (if you do not know how to do this, follow the Unity3D quickstart guide).

Configuring Google Play Games

You can configure Google Play Games from the toolset that is installed with the Google Play Games SDK. You can get to these tools by:

  • Going to the Window tab.
  • Selecting Google Play Games.
  • Going to Setup.
  • Then accessing Android Setup.

Google Play Games toolset Android setup

  • Copy your Application ID from the Linked apps section of Game Services in the Google Play Developer Console, as shown in the example provided below.

Google Play Developer Console Linked apps

  • Then, populate your app_id and package_name in the resource definition, as shown below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_id">[your app_id]</string>
<string name="package_name">[package name]</string>
</resources>

Note

The Client ID that is generated for you when you link the App (shown in this image) is NOT the correct one!

Google Play Developer Console Web App Client ID

In order to get the Google Sign-In working with PlayFab, you need to use a Web Client ID. To obtain your Web Client ID, you must link a Web Application in the Game Services section of the Google Play Console under the Services and API section.

Google Play Developer Console game services

Then configure your app for the web.

Google Play Developer Console link to Web app configuration

From here, you enter your API Endpoint which can be found in PlayFab:

  • Look under Settings.
  • Select the API tab.
  • Go to the API endpoint field.

Google Play Developer Console enter API endpoint

  • Enter this endpoint https://<titleid>.playfabapi.com in the Launch URL field.
  • Select the Save button.
  • This will provide you with the proper Client ID.

To obtain your secret key, follow the link to your API Console.

Link to API Console

  • Select your Web Application (Type).
  • The Client Secret is at the top.

Important

You must change the default callback for Authorized redirect URIs. The proper URI is https://oauth.playfab.com/oauth2/google. If you do not do this step you will get a mismatch redirect_uri error.

PlayFab Google Sign-In setup

PlayFab has its own process for setting up a Google sign-in. You can do this in your PlayFab developer account. If you don't have one, you can sign up for a free developer account.

To set up Google in PlayFab:

  • Navigate to the Add-ons section in your dashboard.
  • Go to the Google Add-On.
  • From there, select the Install Add-on button.

There are a few things you need to enter into this form:

  • The Google App Package ID - This is the name of your Android package. For example, our sample uses com.playfab.gpgexample.
  • The Google App License Key - While this is a required field, it is not related to the Google Sign-In. This field is for setting up a Receipt Validation using our Receipt Validation APIs.

Note

You can get this License Key from the Google Developer Console under the Services & APIs section. Look for a section entitled YOUR LICENSE KEY FOR THIS APPLICATION. Copy and paste the Base64-encoded RSA Public Key into this field.

  • The Google OAuth Client ID - This is the Web Client ID that you used in the Google Play Games SDK setup in Unity.
  • The Google OAuth Client Secret - This is the secret key that you obtained in the last step.

Tip

Be sure to Save Settings!

Adding Google sign-in to your Unity Game

The code to get Google sign-in working is fairly minimal. There is the initialization code, which you will put in your Start (or anywhere you are initializing or starting up your app).

This can be in different places, depending on the architecture and framework of your game or app. In any case, the code below will initialize Google Play Games.

Note

Make sure that you don't forget the using statements, so that you can use the Google Play Games SDK.

// These using statements are required.
using GooglePlayGames;
using GooglePlayGames.BasicApi;
// The following grants profile access to the Google Play Games SDK.
// Note: If you also want to capture the player's Google email, be sure to add
// .RequestEmail() to the PlayGamesClientConfiguration
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
.AddOauthScope("profile")
.RequestServerAuthCode(false)
.Build();
PlayGamesPlatform.InitializeInstance(config);

// recommended for debugging:
PlayGamesPlatform.DebugLogEnabled = true;

// Activate the Google Play Games platform
PlayGamesPlatform.Activate();

The next set of code, the OnSignInButtonClicked() function shown in the example provided below, is bound to a Sign In button in the UI in our example.

Google Sign-In button screen

    private void OnSignInButtonClicked()
    {
        Social.localUser.Authenticate((bool success) => {

            if (success)
            {

                GoogleStatusText.text = "Google Signed In";
                var serverAuthCode = PlayGamesPlatform.Instance.GetServerAuthCode();
                Debug.Log("Server Auth Code: " + serverAuthCode);

                PlayFabClientAPI.LoginWithGoogleAccount(new LoginWithGoogleAccountRequest()
                {
                    TitleId = PlayFabSettings.TitleId,
                    ServerAuthCode = serverAuthCode,
                    CreateAccount = true
                }, (result) =>
                {
                    GoogleStatusText.text = "Signed In as " + result.PlayFabId;

                }, OnPlayFabError);
            }
            else
            {
                GoogleStatusText.text = "Google Failed to Authorize your login";
            }

        });

    }

The OnSignInButtonClicked() code can be a bit overwhelming, so let's break down exactly what is happening:

  • First, we log in with Social.localUser.Authenticate - This will return a boolean if we are able to successfully login. Be aware that if you have not added your test account to your app, this will always return False when testing.
  • When this happens - important step here - add your test account.
  • The link will take you to instructions on adding testers to your Google application.

Note

This is what will trigger the Google Play Games sign-in dialog to pop-up.

  • Next, if we have a successful login, we make a call to PlayGamesPlatform.Instance.GetServerAuthCode, and store it in a variable.
  • Lastly, we make the LoginWithGoogleAccount call, and pass the AuthCode we just got from Google in as the ServerAuthCode parameter.

There is a callback which will have the results of your login to PlayFab. At this point, you should see a successful login in your dashboard!

Google Play Games Sign-In Auth Example

Hopefully, this guide has helped you. But if you have questions, feel free to ask them in our community forums.

You can download the sample for this tutorial here.