This article elaborates on the basic steps of how to authenticate end-users with their Google credentials through Google Play Game in a Unity Android app example. If you want to use OpenID Connect to authenticate end-users with their Google credential, check out the article -- Authentication - Google (OpenID) for detail.
Prerequisites
- You have a Google Play developer account 
- Your testing device with Android API level 19 and above 
Step 1: Create an application on Unity editor
- Open Unity hub and create a new project. 
- Download and import the latest brainCloud client Unity package to this project. Once imported the plugin correctly, you will find the brainCloud tab appears on the editor menu. 
- Download and import the latest - Google Play Games plugin for Unityfrom repositories of Google Play Game Services. (- for the time being, version is 0.10.12)
- Download and import the latest - Google Sign-In Unity Pluginfrom repositories of Google Samples. We will use it to require- server auth codesthat will be passed to brainCloud method- AuthenticateGooglein our code example. (Note that if you see some files already exist in your project, just uncheck them. Also, in some cases, the PlayServiceResover might cause errors in your project, you can uncheck them too) -- [ Note that this step is just used as an extra guarantee mechanism to assure your app can get- ServerAuthCodefrom users Google login. It is ONLY necessary in the case that you cannot get- ServerAuthCodefrom- PlayGamesPlatformin certain versions of- Google Play Games plugin for Unitythat users reported, otherwise, totally ignore it and the related code block].
- After plugins installed, you should be able to find - Google Play Gamesmenu appears under- Windowtab, we will use it to configure- Android setuplater afterward a game created on- Google Play Console.
- You should find these plugins imported folder under the asset. 
- Open - brainCloud settingfrom the tab, select or create a brainCloud back-end app linked to your project.
- Functions and the code that linked behind the buttons are similar to below. Once end-users login into their Google account with Google Play Games by setting - UseGameSignInas- true, so we can get their- userIdvia calling- GetUserId() from success callback, and get their- server auth codesfrom- Google Sign inAPI, then pass them to brainCloud Google authentication method --- AuthenticateGoogle
public void RegisterDeviceToken()
{
GoogleSignIn.Configuration = configuration;
GoogleSignIn.Configuration.UseGameSignIn = true;
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(GoogleSinInCallback);
}
public void GoogleSinInCallback(Task<GoogleSignInUser> task)
{
if (task.IsFaulted)
{
bcreturn.GetComponent<Text>().text = "Failed to call Google SignIn()";
}
else
{
serverAuthCode = task.Result.AuthCode;
}
}
public void GetGoogleUserId()
{
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
.RequestIdToken()
.RequestServerAuthCode(false)
.Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.Activate();
Social.localUser.Authenticate((bool success) => {
if (success)
{
googleId = PlayGamesPlatform.Instance.GetUserId();
channelid.GetComponent<InputField>().text = googleId;
}
else
{
bcreturn.GetComponent<Text>().text = "Failed to get Signed in user info";
}
});
}
public void AuthenticateBC()
{
if (!string.IsNullOrEmpty(googleId))
{
_bc.AuthenticateGoogle(googleId, serverAuthCode, true, authSuccess_BCcall, authError_BCcall);
}
else
{
bcreturn.GetComponent<Text>().text = "log into your Google account first!";
}
}
- Finish the rest callbacks code in your script. Check the completed code from our GitHub Google Authentication project example for reference. (Note that, - GoogleSignInConfigurationrequires- WebClientIdto represent user in order to call Google SignIn, we will need to config this in our code after the- client credentialcreated from- Google Cloud Consolelater.)
- Now, click - Build Settingsfrom Unity- Filetab, switch platform to- Android. Then, create a keystore from- Keystore Managerand save it to your local if you don't have one.
- Set up your app - package nameas well via- Player Settings.
- Now we can move to the next step to create a Google project. 
Step 2: Create a Google project and configure it with OAuth 2.0 Client Credential
- Before creating a project, let's generate a - SHA1fingerprint by using the- keystoreand- package namethat we created from the previous step via the local terminal by using Java JDK- keytoolcommand- keytool -exportcert -alias your-keystore-alia -keystore path-to-your-keystore-file -list -v. ( if not set this command globally, to use this- keytoolcommand you might need to cd into your java JDK home folder)
- Then, instead of going to Google Cloud Console to create a project and add auth client credential manually, we can go to the android-developer - Google Sign Inpage and create a project from there. (by utilizing this, a Google project and Android client ID for OAuth 2.0 created in just one click), paste your Android app- package nameand- SHA1fingerprint accordingly when asked and create the project.
- Now, open Google Cloud Platform, you should be able to see both project and OAuth 2.0 Client IDs created for you. 
- Click the - main navigation menuopen- APIs & Servicespage from GCP console. Click- ENABLE APIS AND SERVICESbutton, search- Google Play Game Servicesand enable it, you should be able to see it listed on the dashboard after enabled. (Note that after you tested your app when completed, you will be able to see several some request traffic generated like the picture shown below).
- Now, we finished the configurations for our project from GCP, note down Client ID and its secret of Web client credential, also, the - project number, we will need them to configure in brainCloud console. (Note that you may already notice, the project number actually prefixed to Client ID)
Step 3: Configure Google client credential on brainCloud
- Open your corresponding brainCloud app from brainCloud development portal, navigate to Design | Core App Info page. 
- Click Google under Configure Platforms and paste your - Client IDand its- secret ofWeb client credential, also the Google- project number, into Google configs fields respectively.
- Make sure the - Google Androidplatform is selected from the Design | Core App Info | Platforms page.
- Now, we finished the settings from brainCloud, let us move to the next step to create a game in Google Play Console. 
Step 4: Create a Google Play Game in Google Play Console
- Log into your Google Play Console and create a new app. 
- Go to the Configuration page under - Play Games Serviceslink your GCP project to this new game by selecting the project from the- cloud projectdropdown menu and click- Use.
- After that, click - Add credential, select GCP project's- OAuth client, click- Save changes, now, you should see the Android client credential is added to this game.
- The screen of - Play Games Services configurationshould be like this
- Go ahead to create several testers for your game (since we will not publish this game to the public, for demonstration purposes, you have to create testers to test Google sign in with their Google accounts) by selecting the Testers menu. 
- Create one dummy game resource from any resources ( Achievements, Events, or Leaderboards) listed under Play Games Services, we will use it to set up Google Play Game connection from Unity editor, the leftover setting from step 1. (for this example, we will create an Achievements resource) 
- Click - Get resourcesbutton from Achievements list, copy down the- Android (XML)format content.
Step 5: Final set up for Android app in Unity and build test app
- Go back to your Unity editor, click Android setup from the Google Play Games menu, paste the resource content from the previous step and GCP Web client ID respectively to pop-up setting window as shown following. 
- Referencing this Client ID as Google SignIn config in your code too. 
- Go to the - Build Settingsscreen and build APK for your test device.
Step 6: Test
- Install APK which built from the previous step to your device and test it. You may be asked to install Google Play Games if it did not exist in your test Android device. Install it and login into one of your Play Game Services tester account. 
- Grant test app permission to your test account. 
- Log into brainCloud after successfully logged in Google account. 
- Check the authenticated tester user from the brainCloud console. 































