Authenticate in Unity Using Google Play Games Services

You can use Google Play Games services to sign in players to an Android gamebuilt on Firebase and Unity. To use Google Play Games services sign-in withFirebase, first sign in the player with Google Play Games, and request anOAuth 2.0 auth code when you do so. Then, pass the auth code toPlayGamesAuthProvider to generate a Firebase credential, which you can use toauthenticate with Firebase.

Important: You can use Google Play Games services sign-in only on Android.

Before you begin

Set up your Unity project

  1. Add the Firebase config file and theFirebaseUnity SDK to your Unity projectas described inAdd Firebase to your Unity project.Follow the instructions for Android.

    Be sure to importFirebaseAuth.unitypackage.

  2. In the Unity Editor, underBuild Settings > Player Settings > OtherSettings set the Android package name of your game.

  3. Then, underBuild Settings > Player Settings > Publishing Settings,select or create a keystore and key, which will be used to sign your Androidpackage. Your APK must be signed for Play Games sign-in to work—thisrequirement applies not just for publishing, but also during development ofyour game.

Set up your Firebase project

  1. In theFirebase console, go to the Firebase projectin which you registered your Unity project.

  2. Set your game's SHA-1 fingerprint from theSettings pageof theFirebase console, using the key you set in Unity.

    You can get the SHA-1 fingerprint of your key with thekeytool command:

    keytool -exportcert -list -v \    -aliasYOUR-KEY-NAME -keystorePATH-TO-KEYSTORE

    Alternatively, you can get the SHA hash of your signing certificate with thegradlesigningReport command:

    gradlew signingReport

    Your APK must be signed with this key, including during development.

  3. EnableGoogle Play Games as a sign-in provider:

    1. In theFirebase console, open theAuthentication section.

    2. Generate and obtain your project's web server client ID and clientsecret:

      1. Within theSign in method tab, enable theGoogle sign-inprovider.

      2. Copy the web server client ID and secret from theGoogle sign-inprovider.

    3. Within theSign in method tab, enable thePlay Gamessign-in provider, and specify your project's web server client ID andclient secret, which you got in the last step.

ConfigurePlay Games services with your Firebase app information

  1. In theGoogle Play Console,open yourGoogle Play app or create one.

  2. In theGrow section, clickPlay Games services > Setup & Management > Configuration.

  3. ClickYes, my game already uses Google APIs, select your Firebaseproject from the list, and then clickUse.

  4. On thePlay Games services configuration page, clickAdd Credential.

    1. Select theGame server type.
    2. In theOAuth client field, select your project's web client ID. Besure this is the same client ID you specified when you enabledPlay Games sign-in.
    3. Save your changes.
  5. Still on thePlay Games services configuration page, clickAdd Credential again.

    1. Select theAndroid type.
    2. In theOAuth client field, select your project's Android client ID.(If you don't see your Android client ID, be sure you set your game'sSHA-1 fingerprint in theFirebase console.)
    3. Save your changes.
  6. On theEvents,Achievements, andLeaderboards pages, create anyPlay Games resources you want to use with your game (if you don'twant to use any immediately, you can create a placeholder entry). Then, onany of theEvents,Achievements, orLeaderboards pages, clickGet resources and copy the Android resources snippet somewhereconvenient. You will need the snippet to set up theGoogle Play Games servicesplugin.

    The resources snippet looks like the following example:

    <?xmlversion="1.0"encoding="utf-8"?><!--GooglePlaygameservicesIDs.Savethisfileasres/values/games-ids.xmlinyourproject.--><resources><!--app_id--><stringname="app_id"translatable="false">123456789000</string><!--package_name--><stringname="package_name"translatable="false">com.example.game</string><!--eventWipedRaid--><stringname="event_wiped_raid"translatable="false">CgkIpKjv1a4PEAIYBA</string></resources>
  7. On theTesters page, add the email addresses of any users who needto be able to sign in to your game before you release it on thePlay Store.

Integrate Play Games sign-in into your game

  1. Download the latest release of thePlay Games plugin for Unityand extract it.

  2. Import the plugin's Unity package into your Unity project. You can find theUnity package in thecurrent-build directory of the release archive.

  3. Set up the Play Games plugin:

    1. ClickWindow > Google Play Games > Setup > Android Setup to open theAndroid Configuration screen.
    2. Paste the Android resources snippet you got from the Play console intotheResources Definition field.
    3. Paste your web server client ID, which you provided when you enabledPlay Games sign-in in theFirebase console, into theClient IDfield.
    4. ClickSetup.
  4. In your game, configure a Play Games client with theRequestServerAuthCodesetting enabled:

    usingGooglePlayGames;usingGooglePlayGames.BasicApi;usingUnityEngine.SocialPlatforms;usingSystem.Threading.Tasks;PlayGamesClientConfigurationconfig=newPlayGamesClientConfiguration.Builder().RequestServerAuthCode(false/* Don't force refresh */).Build();PlayGamesPlatform.InitializeInstance(config);PlayGamesPlatform.Activate();
  5. Then, when a player chooses to sign in with Play Games, callSocial.localUser.Authenticate():

    Social.localUser.Authenticate((boolsuccess)=>{// handle success or failure});

Authenticate with Firebase

After you add Play Games sign-in to your game, you can use the auth code fromPlay Games services to authenticate with Firebase.

  1. After the player has successfully signed in using Play Games, in the sign-incontinuation handler, get an auth code for the player's account:

    Social.localUser.Authenticate((boolsuccess)=>{if(success){authCode=PlayGamesPlatform.Instance.GetServerAuthCode();}});
  2. Then, exchange the auth code from Play Games services for a Firebasecredential, and use the Firebase credential to authenticate the player:

    Firebase.Auth.FirebaseAuthauth=Firebase.Auth.FirebaseAuth.DefaultInstance;Firebase.Auth.Credentialcredential=Firebase.Auth.PlayGamesAuthProvider.GetCredential(authCode);auth.SignInAndRetrieveDataWithCredentialAsync(credential).ContinueWith(task=>{if(task.IsCanceled){Debug.LogError("SignInAndRetrieveDataWithCredentialAsync was canceled.");return;}if(task.IsFaulted){Debug.LogError("SignInAndRetrieveDataWithCredentialAsync encountered an error: "+task.Exception);return;}Firebase.Auth.AuthResultresult=task.Result;Debug.LogFormat("User signed in successfully: {0} ({1})",result.User.DisplayName,result.User.UserId);});

Next steps

After a user signs in for the first time, a new user account is created andlinked to their Play Games ID. This new account is stored as part of yourFirebase project, and can be used to identify a user across every app in yourproject.

In your game, you can get the user's Firebase UID from theFirebase.Auth.FirebaseUser object:

Firebase.Auth.FirebaseUseruser=auth.CurrentUser;if(user!=null &&user.IsValid()){stringplayerName=user.DisplayName;// The user's Id, unique to the Firebase project.// Do NOT use this value to authenticate with your backend server, if you// have one; use User.TokenAsync() instead.stringuid=user.UserId;}

In your Firebase Realtime Database and Cloud Storage Security Rules, you can getthe signed-in user's unique user ID from theauth variable, and use it tocontrol what data a user can access.

To get a user's Play Games player information or to access Play Games services,use the APIs provided by the Play Games plugin.

To sign out a user, callSignOut():

auth.SignOut();

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-18 UTC.