Blog

Latest news and updates from the PlayFab developers

by MarcoWilliams 2017-02-01

Using Google Sign-in with PlayFab

NOTE:  This post is obsolete.  Please follow the directions here instead.  Tutorial

First, I would like to start off by saying that Google Sign-In has changed quite a bit in the last year or so and we have been tracking the changes pretty closely. The recent changes to Google Play Games and deeper integrations with Firebase have introduced some challenges with the Google Sign-In process with PlayFab and I am totally stoked to announce that we have a great solution as we move forward with the new Google Play Games and Firebase FCM era.

We will be releasing some additional info on our Documentation Site in the form of official tutorials, but here in this blog post you will find all that you need to get Google Sign-In working in your game.

Prerequisites

There are a few things you need to know before getting started.

  1. This post / guide is targeted at the integration with Unity3D so you will need Unity installed.
  2. You must have a Google Play developer account.
  3. It is preferred that you already have a game created in the play.google.com/apps/publish dashboard (although it is not a requirement).
  4. You must have a Game Title created in your PlayFab developer dashboard.
  5. You must have the PlayFab Unity SDK installed in your project.

Setup for Google

We are going to start here because without having Google set up properly, it gets a little confusing on where to get certain IDs and keys. To set up Google, you will first need to create a “Project” and then you will need to create application credentials.

  1. Navigate in a browser to Google Developer Console.
  2. If you have not created a project for your Game, then do so now by selecting Create Project. Or you can create a project from your game found in Play Console by clicking View More Projects.

    Create Google Project

  3. Now select Credentials from the left hand menu.

  4. You will need to complete the OAuth Consent Screen details, so click on OAuth Consent Screen and fill out your email, product name, and any additional information you want to show on the OAuth Concent Screen. ** A little tip, this is generally for websites and what you put here does not really show up in your game. Regardless, you should put valid info in here.

  5. Click Save.

  6. Next, click on the Credentials tab and select OAuth Client ID from the Create Credentials dropdown menu.

    Create OAuth Client ID

  7. Now you have to fill out some important information. Enter into the Authorized JavaScript origins field a URI in this format https://{{TitleId}}.playfabapi... you can find your TitleId in your PlayFab Dashboard. For example, my BigOleSampleProject TitleId is 6CA5. So its URI looks like this: https://6CA5.playfabapi.com.

  8. Also, you will need to fill out the Authorized redirect URIs field and enter the following URL: https://oauth.playfab.com/oauth2/google This is the same for all titles.

    Configure OAuth Credentials

9.Take note of your ClientID & Client Secret! You will need these for your next steps!

Configure your Game Title in PlayFab

Now that you have configured your Google Project, you will also need to configure your PlayFab title to support login with Google in your game.

  1. Login to your developer account.
  2. Navigate to Add-ons –> Google.
  3. You will need to popluate all 4 fields. The first two are not related to Google authentication, but they are necessary for Google Receipt Validation.

    Google App Package ID

    • Google App License Key: This is your license key from google to allow you to verify purchases. This can be found in the Services & APIs section of theGoogle Play Developer Console

    Google App License Key

    • Google OAuth Client ID: This is the Client ID that you just created in your Google Project.
    • Google OAuth Client Secret: This is the secret key that you just created in your Google Project.

    Google Client & Secret

Now a little bit of Unity3D Setup for Google

When setting this up with Unity3D, I use Google Play Games unity package which makes it very easy to set up Google Plus Sign-in.

  1. Download and import the Google Play Games package into your project.
  2. Run the Google Play Games Setup for Android.Google Play Games Setup

  3. Constants Class Name is your Project Name (or name of your game).

  4. In the resource Definition you need to add the following xml code. Be sure to replace the app_id and the package_name with your own.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="app_id">166262043182</string>
    <string name="package_name">com.playfab.BigOleSampleProject</string>
    </resources>
    

    Note that your App_Id is the first part of your ClientID.AppId from Client ID

  5. Populate the ClientID in the ClientID field, and you should end up with something like the following:

    Google Play Games Setup Complete

  6. Click Setup.

Login With Google using PlayFab

The following code does 3 things, it initializes the the Google Play Games Platform; Authenticates the user using Google Sign-In and then gets a ServerAuthCode which we can then send for authentication to PlayFab. The parts of this code that you really need is the code inside the Start() method. You can use this code anywhere you are implementing your authentication in your own game.

  1. Create a new C# script called SetupGPG.cs and add it to a game object in your scene.
  2. Copy and paste the following code into SetupGPG.cs.
using UnityEngine;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using PlayFab;
using PlayFab.ClientModels;
public class SetupGPG : MonoBehaviour {
    // Use this for initialization
    void Start () {
        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
        .AddOauthScope("profile")
        .Build();
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.Activate();
        Social.localUser.Authenticate((success) => {
            if (success)
            {
                PlayGamesPlatform.Instance.GetServerAuthCode((code, authToken) => {
                    PlayFabClientAPI.LoginWithGoogleAccount(new LoginWithGoogleAccountRequest()
                    {
                        TitleId = PlayFabSettings.TitleId,
                        ServerAuthCode = authToken,
                        CreateAccount = true,
                    }, (successLoginResult) =>
                    {
                        Debug.LogFormat("Login With Google Success: ", successLoginResult.PlayFabId);
                    }, (errorResult) => {
                        Debug.Log(errorResult.GenerateErrorReport());
                    });
                });
            }
        
        });
    }
}

Conclusion

Setting up Google Sign-in is pretty complex, but once you know the process it’s easy to follow the steps. In this blog we walked you thought what it takes to set up a Google Play project, configure your title in PlayFab, and then connect the two via code in Unity. I hope that this clears up the process of getting Google Sign-In working, but if you have any questions please let us know in our forums, and we’ll get you all the info you need.