Authentication - Google (OpenID)
This article elaborates on the basic steps of how to authenticate end-users with their Google credentials through OpenID Connect in a Unity Android app example. If you want to use Google Play Game to authenticate end-users with their Google credential, check out the article -- Authentication - Google (PlayGame) for detail.
Prerequisites
- You have a Google account to create projects in GCP
- 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 Sign-In Unity Pluginfrom repositories of Google Samples. We will use it to requireserver auth codesthat will be passed to brainCloud methodAuthenticateGooglein our code example.
info
Some files under PlayServiceResover might be obsolete, most likely, you will need to install the unity-jar-resolver plugin from Google Samples repositories to update the PlayServiceResover. Also, in case that you encounter duplicated Task DLL file error, just go ahead to delete the one that was installed with the plugin located under Asset->Paese->Plugins folder


- Open
brainCloud settingfrom the tab, select or create a brainCloud back-end app linked to your project.


- Functions and the code behind the buttons are similar to below. Once end-users login into their Google account with Google Play Games by setting
UseGameSignInasfalse, so we can get theiruserIdvia callingGetUserId() from success callback, and get theirserver auth codesfromGoogle Sign inAPI, then pass them to brainCloud Google authentication method --AuthenticateGoogleOpenId
public void GoogleAPISignIn()
{
bcreturn.GetComponent<Text>().text = "Google sign in......";
GoogleSignIn.Configuration = configuration;
//GoogleSignIn.Configuration.UseGameSignIn = true;
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(GoogleSinInCallback);
}
public void GoogleSinInCallback(Task<GoogleSignInUser> task)
{
if (task.IsFaulted)
{
using (IEnumerator<System.Exception> enumerator = task.Exception.InnerExceptions.GetEnumerator())
{
if (enumerator.MoveNext())
{
GoogleSignIn.SignInException error = (GoogleSignIn.SignInException)enumerator.Current;
bcreturn.GetComponent<Text>().text = "Task Exception Error: " + error.Status + " " + error.Message;
}
else
{
bcreturn.GetComponent<Text>().text = "Task Unexpected Exception" + task.Exception;
}
}
}
else if (task.IsCanceled)
{
bcreturn.GetComponent<Text>().text = "Canceled";
}
else
{
//serverAuthCode = task.Result.AuthCode;
googleEmail = task.Result.Email;
idToken = task.Result.IdToken;
bcreturn.GetComponent<Text>().text = "Logged into google! \n Email: " + googleEmail + " \n IdToken: " + idToken;
channelid.GetComponent<InputField>().text = "[" + googleEmail + "]" + idToken;
}
}
- Finish the rest callbacks code in your script. Check the completed code from our GitHub Google Authentication project example for reference. (Note that,
GoogleSignInConfigurationrequiresWebClientIdto represent user in order to call Googel SignIn, we will need to config this in our code after theclient credentialcreated fromGoogle Cloud Consolelater.) - Now, click
Build Settingsfrom UnityFiletab, switch platform toAndroid. Then, create a keystore fromKeystore Managerand save it to your local if you don't have one.

