> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PlayFab auth with Google Play Games Sign-In in Unity

> Step-by-step tutorial for adding Google Play Games Sign-In to a Unity project with the PlayFab SDK, covering Play Console setup and player authentication.

# 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](/services/playfab/sdks/unity3d/quickstart).
* The [Google Play Games SDK](https://github.com/playgameservices/play-games-plugin-for-unity).
* Familiarity with [Login basics and Best Practices](/services/playfab/identity/player-identity/login/login-basics-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.

1. Create an **Application** - If you don't have an application created, you need to do that in the [Google Play Developer Console](https://play.google.com/apps/publish).

2. Build your game AAB, and upload it for internal testing. You can follow [this](https://support.google.com/googleplay/android-developer/answer/9859348?visit_id=638373214207471441-2235763004\&rd=1) 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](https://docs.unity3d.com/Manual/android-keystore-create.html) doc on how to create a keystore.

3. Set up Google Play Games services following the instructions on this page: [Setting Up Google Play Games Services](https://developers.google.com/games/services/console/enabling).

4. Download the [Google Play Games SDK](https://github.com/playgameservices/play-games-plugin-for-unity) and install it.

5. Install the PlayFab Unity SDK (if you don't know how to do this task, follow the [Unity3D quickstart guide](/services/playfab/sdks/unity3d/quickstart)).

## 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**.

  <img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-configuring-google-play-games.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=cc4e610420ae613914a545645f437f28" alt="Configuring Google Play Games" width="698" height="156" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-configuring-google-play-games.png" />
* The Android configuration window shows up, we need to input the Resources Definition and the Web App Client ID on this screen.

  <img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-paste-android-resources-empty.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=b13a6bfbd822a8442c007a5163769cf3" alt="Paste Android Resources Empty" width="653" height="578" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-paste-android-resources-empty.png" />

### Getting Resources Definition

* Follow [these](https://developer.android.com/games/pgs/unity/unity-start#get-android-resources) 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](https://developer.playfab.com/).

* Select your title.

* Go to your title settings by clicking on the **gear icon** next to the title name and select **Title Settings**.

  <img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-pf-title-settings.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=54512b7841fc8a5d274f0012efca8998" alt="PF Title Settings" width="290" height="223" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-pf-title-settings.png" />

* Select the **API Features** tab.

* Copy to the **API endpoint** field.

  <img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-pf-api-endpoint.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=80e6521a94996698755cfe971e321438" alt="Google Play Developer Console enter API endpoint" width="539" height="293" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-pf-api-endpoint.png" />

Now that you know your title API Endpoint, follow the steps outlined [here](https://developers.google.com/workspace/guides/create-credentials#web-application) for creating the OAuth Client ID Credential.

* When creating the Client ID, you need to add the API endpoint as an **Authorized JavaScript origin** for your Client ID.

  <img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-authorized-javascript-origins.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=9a2f0d445c948f5386bae1350a4d5074" alt="Authorized Javascript Origins" width="504" height="215" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-authorized-javascript-origins.png" />

* 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.

  <img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-authorized-redirect-uris.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=dc66e1b57e200b52f04f5f0ac13f16a2" alt="Authorized Redirect URIs" width="514" height="206" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-authorized-redirect-uris.png" />

After creating the Client ID, you'll get a screen similar to the following one, where you'll get the Client ID string generated and a Client Secret.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-client-id-created.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=87ed059a93128acb745348cdfb97e859" alt="OAuth Client ID created" width="500" height="540" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-client-id-created.png" />

Now, you can go back to the Android Configuration window in Unity and paste the client ID in the **Client ID** textbox:

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-paste-client-id.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=e880647b4bccd88ff590b840b8d1f642" alt="Paste Client ID" width="651" height="119" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-paste-client-id.png" />

Now that both values are populated, you can select **Setup**:

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-setup-android-configuration.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=83928abc8c46b9fac2698afc9f069219" alt="Setup completed Android configuration" width="653" height="582" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-setup-android-configuration.png" />

## 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](https://developer.playfab.com).

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.

There are a few things you need to enter into this form:

* 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**.

<Note>
  You can get this License Key from the [Google Developer Console](https://play.google.com/apps/publish), 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.
</Note>

* 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](https://developers.google.com/workspace/guides/create-credentials#create_a_service_account) steps. Then, create credentials for that service account following the steps [here](https://developers.google.com/workspace/guides/create-credentials#create_credentials_for_a_service_account). After you get the credentials JSON file, paste the whole JSON content in this field.

The add-on configuration should look like this before saving:

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-google-addon-config.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=481bc6a5ddebf25c3194630e2444c753" alt="Google Add-on Setup in PlayFab" width="550" height="713" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/google-unity/google-unity-google-addon-config.png" />

<Tip>
  Be sure to **Save Settings**!
</Tip>

## 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.

<Note>
  Make *sure* that you don't forget the **using** statements, so that you can use the Google Play Games SDK.
</Note>

```csharp theme={null}
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;

public class PFGoogleSignInUnity : MonoBehaviour
{
    void Start()
    {
        PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication);
    }

    internal void ProcessAuthentication(SignInStatus status)
    {
        if (status == SignInStatus.Success)
        {
            PlayGamesPlatform.Instance.RequestServerSideAccess(false, ProcessServerAuthCode);
        }
    }

    private void ProcessServerAuthCode(string serverAuthCode)
    {
        Debug.Log("Server Auth Code: " + serverAuthCode);

        var request = new LoginWithGooglePlayGamesServicesRequest
        {
            ServerAuthCode = serverAuthCode,
            CreateAccount = true,
            TitleId = PlayFabSettings.TitleId
        };

        PlayFabClientAPI.LoginWithGooglePlayGamesServices(request, OnLoginWithGooglePlayGamesServicesSuccess, OnLoginWithGooglePlayGamesServicesFailure);
    }

    private void OnLoginWithGooglePlayGamesServicesSuccess(LoginResult result)
    {
        Debug.Log("PF Login Success LoginWithGooglePlayGamesServices");
    }

    private void OnLoginWithGooglePlayGamesServicesFailure(PlayFabError error)
    {
        Debug.Log("PF Login Failure LoginWithGooglePlayGamesServices: " + error.GenerateErrorReport());
    }
}
```

Let's break down exactly what is happening:

* 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](https://developers.google.com/games/services/console/testpub) guide to add testers to your Google application.

<Note>
  The call to `PlayGamesPlatform.Instance.Authenticate` is what will trigger the **Google Play Games** sign-in dialog to pop-up.
</Note>

* 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.LoginWithGooglePlayGamesServices` call, and pass the `AuthCode` we just got from Google in as the `ServerAuthCode` parameter.

* Lastly, depending on the result of `LoginWithGooglePlayGamesServices` call, either `OnLoginWithGooglePlayGamesServicesSuccess` or `OnLoginWithGooglePlayGamesServicesFailure` is 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!

Hopefully, this guide helped you. But if you have questions, feel free to ask them in our [community forums](https://community.playfab.com/).


## Related topics

- [Migrate from Google Sign-in to Google Play Games in Unity](/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration-details.md)
- [Migrate Unity games to Google Play Games Sign-in](/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration.md)
- [Google Sign-in to Play Games sign-in migration fallback](/services/playfab/identity/player-identity/platform-specific-authentication/google-play-games-sign-in-migration-fallback.md)
- [Platform-Specific Authentication](/services/playfab/identity/player-identity/platform-specific-authentication/index.md)
- [PFAuthenticationLoginWithGooglePlayGamesServicesRequest](/services/playfab/api-references/c/pfauthenticationtypes/structs/pfauthenticationloginwithgoogleplaygamesservicesrequest.md)
