Setting up PlayFab authentication using Google Play games sign-in in Unity
Prerequisites
- A project built using the Unity game engine.
- A Google developer account.
- A PlayFab developer account that has access to a game title in PlayFab.
- The PlayFab Unity Editor Extensions & Unity SDK.
- The Google Play Games SDK.
- Familiarity with Login basics and Best Practices.
Getting started
First, we start by setting up some basic things in Google Play. There are a few good guides on how to achieve some of these tasks, so we aren’t going to reinvent the wheel.- Create an Application - If you don’t have an application created, you need to do that in the Google Play Developer Console.
- Build your game AAB, and upload it for internal testing. You can follow this guide on how to release your app to Internal testing track. During the signing process consider that for your app bundle you have to use your own keystore for this purpose. Refer to this doc on how to create a keystore.
- Set up Google Play Games services following the instructions on this page: Setting Up Google Play Games Services.
- Download the Google Play Games SDK and install it.
- Install the PlayFab Unity SDK (if you don’t know how to do this task, 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.
- The Android configuration window shows up, we need to input the Resources Definition and the Web App Client ID on this screen.
Getting Resources Definition
- Follow these steps to get your Android Resources definition.
Getting Web App Client ID
In order to get the Google Sign-In working with PlayFab, you need to use a Web Client ID that you can get by creating an OAuth Client ID credential for “Web application” type. Before creating the OAuth Client ID you need to know your PlayFab API Endpoint, which you can found following these steps:- Log in to Game Manager.
- Select your title.
- Go to your title settings by clicking on the gear icon next to the title name and select Title Settings.
- Select the API Features tab.
- Copy to the API endpoint field.
- When creating the Client ID, you need to add the API endpoint as an Authorized JavaScript origin for your Client ID.
- Also, make sure to add your own application redirect URI as an Authorized Redirect URI. If you don’t do this step, you’ll 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 set up 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 Google button.
- The Google App Package ID - This field is the name of your Android package. For example, our sample uses
com.playfab.gpgexample. - The Google App License Key - While this field is required, it isn’t related to the Google Sign-In. This field is for setting up a Receipt Validation using our Receipt Validation APIs.
You can get this License Key from the Google Developer Console, navigate to your app, under the Monetize section. Select the section entitled Monetization Setup. In here, scroll down until you see a Licensing text box, copy and paste the Base64-encoded RSA Public Key into this field.
- The Google OAuth Client ID -The Web Client ID that you used in the Google Play Games SDK setup in Unity.
- The Google OAuth Client Secret - The Secret Key that you obtained when creating the OAuth Client ID.
- The Service Account Key - This field expects a service account key JSON string, you need to create a service account key following these steps. Then, create credentials for that service account following the steps here. After you get the credentials JSON file, paste the whole JSON content in this field.
Adding Google sign-in to your Unity Game
The code to get Google sign-in working is fairly minimal. There’s the initialization code, which you put in your Start (or anywhere you’re initializing or starting up your app). This code can be in different places, depending on the architecture and framework of your game or app. In any case, the following code initializes Google Play Games.Make sure that you don’t forget the using statements, so that you can use the Google Play Games SDK.
- First, we log in with
PlayGamesPlatform.Instance.Authenticate- This function receives a callback function as a parameter that handles the result, and lets us know if the user was able to successfully login based on the SignInStatus received. - If you don’t add your test account to your app, the callback SignInStatus result is always Canceled when testing. In case this issue happens, follow the steps on this guide to add testers to your Google application.
The call to
PlayGamesPlatform.Instance.Authenticate 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.RequestServerSideAccess, which receives another callback and returns the ServerAuthCode string. -
Then, we make the
PlayFabClientAPI.LoginWithGooglePlayGamesServicescall, and pass theAuthCodewe just got from Google in as theServerAuthCodeparameter. -
Lastly, depending on the result of
LoginWithGooglePlayGamesServicescall, eitherOnLoginWithGooglePlayGamesServicesSuccessorOnLoginWithGooglePlayGamesServicesFailureis called, these callbacks are the ones that handle your login operation result to PlayFab. At this point, you should see a successful login in your dashboard!
