Implementing Sign in with Google is a common frustration among game developers. A lot of games utilize this authentication method. Despite this, there isn't much documentation on this subject out there. That's why we at LootLocker decided to take matters into our own hands. Follow along and let's get it working!
We have split this guide into distinct sections so that you can either read it top to bottom, or go straight to the part you want to learn more about.
This guide assumes that you already have a game project set up and able to build to the platform you will be targeting. Throughout the outlined steps, you will need your app's package name/bundle id (the name is different depending on your target platform). It follows this pattern "com.<organization>.<appname>" and needs to be unique for your game. A few different ways to find yours if you do not already know it are:
This guide also assumes that you have a project set up in LootLocker. LootLocker is the plug and play game backend that makes cross platform feel like one platform. It helps you elevate your game from local to online in a simple way, adding functionality such as cloud saves, cross platform account management, and a versatile economy system, along with much more. If all you are looking for are details on how to add Google Sign In to your game, then you can skip the LootLocker parts of this guide. But if you are curious, you can read more here.
If you plan to release your game to the Google Play Store, then start here. If not, head straight to Using Google Cloud Console. For this you will need to have an Active Google Play Console account.
In the play console you need to have a project configured. Google has some resources on this, and how you configure it depends to a large extent on your game, so we will not go through that here. The only important distinction is that the project you have configured should be a game.
Go to Google Play Console > Play Games Services > Setup and management > Configuration. In the top right, press "View in Google Cloud Platform" and then continue with the credentials part of Using Google Cloud Console.
To set up the prerequisites for authentication using Google you need to have a project for your game configured in the Google Cloud Console. If you already have one for the game you're building (which is automatically the case if you have created a project for your game in Google Play Console) then use that one. Otherwise create a project from the top left project selection dropdown > New Project. Input the organization and project settings applicable for your game.
Now to the part of the google setup that is crucial for authentication and subsequent LootLocker integration. Go to APIs & Services > Credentials. Here you will need to create credentials for all OAuth Client types your game will be utilizing. For Unity, you will only need a Web Client. For Unreal you will also need an Android Client for your Android builds, an iOS Client for your iOS builds, and additional client credentials if you are building for for example UWP, or desktop.
+ Create Credentials
For LootLocker to integrate with your Google project you need to create a service account for the two services to communicate.
+ Create Credentials
lootlocker
Service Account Token Creator
To enable your players to utilize all the power of LootLocker while authenticated with Google you need to configure your game to support Google Sign In.
Go to Platforms and enable Google.
\Assets\{UnityProject}\Editor\m2repository\com\google\signin\google-signin-support\1.0.4
in your file explorerGoogle-signin-support-x.x.x
and enable Android as included Platforms.Create a new C# Script for Signing in with Google and add the reference using Google;
Lets start by creating the Google configuration:
Note: Remember to use your own Web client ID otherwise it will fail.
Note: We also set the configuration directly to the GoogleSignIn class, this is because we cannot modify the configuration once we've called the Default Instance the first time.
In our configuration specify that we want Google Sign In to return the ID Token. The ID Token is what we will later use to authenticate the player with LootLocker.
We also set "useGameSignIn" to false. Setting this to true activates the "Play Games Services" instead of the "stock" Google authentication, which is not what we want in this case.
Now you may be confused on how the function onAuthenticationFinished will be called. It is a callback that you register when starting the sign in process as so:
It would probably be smart to do more error handling in case it does not succeed, but that depends on your project. However, if you were to run this code now, you should see a pop up to select a Google Account and once you've selected, your token should be filled out.
Now that we have gotten a token from Google, we can finally Authenticate with LootLocker and start a session.
And there we go. You should now have authenticated with LootLocker and Google.
You need to have set up Unreal Engine for development of the platform that you will build to. Epic provides some guides on this topic, so make sure that you have properly followed the one for your platform, for example Android or iOS.
Next, you will need to install the "Online Subsystem for Google" (you can read more about Online Subsystems here) plugin for the editor version you're using and copy it into the project plugins folder (it doesn't work with it in the editor plugin folder). You do this by going to the Engine install location and copy the plugin folder (<Engine Install Location>/Engine/Plugins/Online/OnlineSubsystemGoogle) into your projects plugin folder (<Project Folder>/Plugins, if it doesn't exist yet then create it).
In Project Settings > Platforms > Android > APK Packaging
In the file DefaultEngine.ini (or a platform specific ini file if that suits your need) located in (<Project Folder>/Config/DefaultEngine.ini) add these sections with the ids you set up during Google Setup:
[OnlineSubsystemGoogle]
bEnabled=true
GamesAppID=<AppID from GCLOUD>
ClientId=<Android OAuth ClientID from GCLOUD>
ServerClientId=<Web Client ID from GCLOUD>
ClientSecret=<Web Client Secret from GCLOUD>
[OnlineSubsystemGoogle.OnlineIdentityGoogle]
+ScopeFields="https://www.googleapis.com/auth/userinfo.email"
+ScopeFields="https://www.googleapis.com/auth/userinfo.profile"
The Online Subsystems do not natively have any Blueprint exposure, so you will need to delve into C++ to build Google Authentication. This guide will however help you make the blueprint nodes necessary to then build your sign in flow in blueprints if that is more your speed.
Add the generic Online Subsystem to your private dependency modules in your project's build.cs file and add the Google online subsystem to any platforms where you intend to use it.
Create a class that we will extend with the logic we need throughout this guide. Here we've named it GoogleSignIn but you can name it whatever you wish. Place the header in Project Folder/Source/Project Name/Public
and the cpp file in Project Folder/Source/Project Name/Private
To the GoogleSignIn class add an Initialize method that will initialize the google subsystem and keep a pointer to it.
To the GoogleSignIn class add a HasGoogleOnlineSubsystem method that we can use in our sign in flow logic to enable or disable UI related to Google Sign In.
Now to the good part: actual sign in. First, here's the example code:
There are a few things going on in this step:
FGoogleUserData
and FLoginCompletedResponse
classes. The USTRUCT
and UPROPERTY
macros cause the class to be usable in blueprints.SignInWithGoogle
method that takes a local user number for whom to start the login process. Unless your game has multiple local players you will always use 0 here. It also takes a callback that will be called when the login process has completed, either successfully or unsuccessfully. The bool that is returned from this method tells you whether the process started ok or not.LoginCompletedHandler
which handles the response from the google subsystem and extracts the data we want upon login.FDelegateHandle
.SignInWithGoogle
and LoginCompletedHandler
does some error checking, but you may want to add some logging or extra error handling.GetAuthAttribute(AUTH_ATTR_ID_TOKEN, UserData.IdToken);
.The previous steps have all been incremental changes. Here's a full copyable example of the code.
Now that you have the Google Sign In class, you simply need to utilize those methods in a place in your code that suits your project.
For example:
UGoogleSignIn::Initialize
.UGoogleSignIn::HasGoogleOnlineSubsystem
.UGoogleSignIn::SignInWithGoogle
.ULootLockerSDKManager::StartGoogleSessionForPlatform
method specifying the platform you're signing in from, or the ULootLockerSDKManager::StartGoogleSession
if you don't want to specify a specific platform. For this call, you need to use the ID Token that was returned in the UGoogleSignIn::SignInWithGoogle
callback.Use the nodes we created in Add sign in logic to create your sign in flow. Here's an example of what that could look like given the created nodes.
The steps you need are to:
And there you have it - implementing Sign in with Google in both Unity 3D and Unreal Engine. We hope that these guides have made your life considerably easier giving you more time to spend actually making your game. If you liked this guide, please let us know in our Discord channel or on Twitter.