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

# Setting up PlayFab authentication using Facebook and Unity

> Step-by-step tutorial for integrating Facebook login into a Unity project with the PlayFab SDK, covering Facebook app setup and player authentication code.

This tutorial guides you through an example of PlayFab authentication using Facebook and Unity.

## Requirements

* [Unity 5+](https://unity3d.com/) and a created project.
* [Facebook account](https://www.facebook.com/) for testing.
* [Facebook SDK](https://developers.facebook.com/docs/unity/) imported into the project.
* Registered [PlayFab](https://developer.playfab.com/) title.
* [PlayFab SDK](https://aka.ms/playfabunitysdkdownload) imported into the project with a configured title reference.
* Familiarity with [Login basics and Best Practices](/services/playfab/identity/player-identity/login/login-basics-best-practices).

## Implementation

### Registering a Facebook application

Start by navigating to the [Facebook Developer Portal](https://developers.facebook.com/):

1. Move your mouse over the **My Apps** button.
2. Select **Add a New App**, as shown on the picture below.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-add-a-new-app.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=8f2cf49110ee6fa800418dc19843cb16" alt="Facebook add a new app" width="1049" height="502" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-add-a-new-app.png" />

A new **Application** pop-up will open.

1. Enter a name for your **Application**.
2. Enter a **Contact Email**.

<Note>
  Make sure to come up with your own *unique* **Application Name** and **Email**, as shown on the picture below.
</Note>

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-new-app-id.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=7a0feb9725217dcca8d4b0fb861e3c3e" alt="Facebook new app ID" width="766" height="369" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-new-app-id.png" />

1. Navigate to the **Settings** tab.
2. Then move to the **Basic** subtab.
3. Locate your **Application ID**.
   * Copy it somewhere to a safe place that is easily accessed.
     We'll use it later to set up the Facebook SDK.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-save-app-id.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=540b62a0ad44a22f49fa944e2d6741d3" alt="Facebook save app ID" width="1049" height="650" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-save-app-id.png" />

Navigate to the [Facebook Access Token Tool](https://developers.facebook.com/tools/accesstoken/).

1. Locate your **App** in the list.
2. Verify that it has an assigned **User Token**.

<Note>
  If you *don't* have an assigned user token, request one using the button to the right of your screen. Refer to this page whenever you need a fresh user token. If authorization fails during testing on a PC, token expiration might be the most likely reason. Refresh the page to get a new user token and test again.
</Note>

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/access-token-tool.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=8bcd65dadccacafbfe04227bd3d5ad4c" alt="Facebook Access Token Tool" width="1094" height="406" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/access-token-tool.png" />

1. On your **Application Manager** page, navigate to **+ Add Products**.
2. Locate the **Facebook Login** entry and select **Get Started**.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-html5/login-get-started.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=bd346c82af2a1914bd8dfca79653f5c6" alt="Facebook login get started" width="1205" height="601" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-html5/login-get-started.png" />

A page with **Login** product settings should open.

1. Make sure that you have the **Login** product **Settings** page opened.
2. Verify that both **Client OAuth** and **Web OAuth** are on.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-html5/login-product-settings.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=493c1ca8600489493ca47ced5c579204" alt="Facebook login product settings" width="1204" height="915" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-html5/login-product-settings.png" />

### Setting up Facebook SDK

Open your **Unity Project**:

1. Select **Facebook** in the file menu.
2. Then select **Edit Settings**.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/edit-project-settings.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=fbf61a04fb83b8ada1e829f7918cdc14" alt="Edit Facebook Unity project settings" width="518" height="483" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/edit-project-settings.png" />

The **Inspector** window will display the current **Facebook SDK** configuration.

* Enter your **Application ID** in the corresponding field, as shown in the following picture.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/enter-app-id.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=46683d51640caa63de8f3c828b2f7adf" alt="Enter app ID in Facebook SDK configuration" width="477" height="528" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/enter-app-id.png" />

### Simple authentication script

Create a new **Game Object**, and rename it to **PlayfabFacebookAuthExample**, as shown in the following picture.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/create-game-object.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=3a19b0150725400f2affedef4c8c0ab8" alt="Create a new Game Object" width="446" height="342" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/create-game-object.png" />

1. Create a script called **PlayfabFacebookAuthExample.cs**.
2. Add it to the **Game Object** as shown on the picture below.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/add-example-script.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=df63197b8889dcd5fccadabef66e37e0" alt="Add example script to game object" width="1025" height="444" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/add-example-script.png" />

Inside  `PlayfabFacebookAuthExample.cs` place the following code.

```csharp theme={null}
// Import statements introduce all the necessary classes for this example.
using Facebook.Unity;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
using LoginResult = PlayFab.ClientModels.LoginResult;

public class PlayfabFacebookAuthExample : MonoBehaviour
{
    // holds the latest message to be displayed on the screen
    private string _message;

    public void Start()
    {
        SetMessage("Initializing Facebook..."); // logs the given message and displays it on the screen using OnGUI method

        // This call is required before any other calls to the Facebook API. We pass in the callback to be invoked once initialization is finished
        FB.Init(OnFacebookInitialized);  

    }

    private void OnFacebookInitialized()
    {
        SetMessage("Logging into Facebook...");

        // Once Facebook SDK is initialized, if we are logged in, we log out to demonstrate the entire authentication cycle.
        if (FB.IsLoggedIn)
            FB.LogOut();

        // We invoke basic login procedure and pass in the callback to process the result
        FB.LogInWithReadPermissions(null, OnFacebookLoggedIn);
    }

    private void OnFacebookLoggedIn(ILoginResult result)
    {
        // If result has no errors, it means we have authenticated in Facebook successfully
        if (result == null || string.IsNullOrEmpty(result.Error))
        {
            SetMessage("Facebook Auth Complete! Access Token: " + AccessToken.CurrentAccessToken.TokenString + "\nLogging into PlayFab...");

            /*
             * We proceed with making a call to PlayFab API. We pass in current Facebook AccessToken and let it create
             * and account using CreateAccount flag set to true. We also pass the callback for Success and Failure results
             */
            PlayFabClientAPI.LoginWithFacebook(new LoginWithFacebookRequest { CreateAccount = true, AccessToken = AccessToken.CurrentAccessToken.TokenString},
                OnPlayfabFacebookAuthComplete, OnPlayfabFacebookAuthFailed);
        }
        else
        {
            // If Facebook authentication failed, we stop the cycle with the message
            SetMessage("Facebook Auth Failed: " + result.Error + "\n" + result.RawResult, true);
        }
    }

    // When processing both results, we just set the message, explaining what's going on.
    private void OnPlayfabFacebookAuthComplete(LoginResult result)
    {
        SetMessage("PlayFab Facebook Auth Complete. Session ticket: " + result.SessionTicket);
    }

    private void OnPlayfabFacebookAuthFailed(PlayFabError error)
    {
        SetMessage("PlayFab Facebook Auth Failed: " + error.GenerateErrorReport(), true);
    }

    public void SetMessage(string message, bool error = false)
    {
        _message = message;
        if (error)
            Debug.LogError(_message);
        else
            Debug.Log(_message);
    }

    public void OnGUI()
    {
        var style = new GUIStyle { fontSize = 40, normal = new GUIStyleState { textColor = Color.white }, alignment = TextAnchor.MiddleCenter, wordWrap = true };
        var area = new Rect(0,0,Screen.width,Screen.height);
        GUI.Label(area, _message,style);
    }
}
```

## Testing

### Running the application in the editor

1. Select the **Play** button. Once **Facebook** is initialized (and an authentication call is made), a **Facebook** authentication dialog will appear in your game view.
2. Enter the **User Token**.
3. Select the **Send Success** button, as shown in the following picture.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/enter-user-token.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=b3384d650c0264c549d2ef1780c4eb98" alt="Enter user token in Facebook Auth dialog" width="903" height="581" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/enter-user-token.png" />

* Check the console output. It should render our debug statements, as shown on the picture below.
* If you see no errors, this means that authentication was successful.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/debug-console-output.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=720db9bf8bc65ead9c9ff196c817a2fe" alt="Debug console output" width="698" height="389" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/debug-console-output.png" />

### Running the application on Android

Before launching the application on Android, you're required to perform additional configuration steps:

* First, you need to ensure you have the Android platform selected, and the unique **Package ID** set (as indicated in the example below.

<Note>
  Make sure to create your own *unique* **Package ID**.
</Note>

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/set-android-package-id.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=e8bdadc7a8f8bf9ef4318a55f2d28338" alt="Set Android package ID" width="1178" height="617" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/set-android-package-id.png" />

<Tip>
  **Optional step**: Install [Open SSL](https://wiki.openssl.org/index.php/Binaries) and add it to the **PATH** environment variable. If it's missing, Unity will produce a harmless error during the build. This error doesn't\* stop the build, or effect the execution.
</Tip>

Next, in your **Facebook Application** console:

1. Navigate to **Basic** settings.
2. Select **+ Add Platform**, as indicated in the example provided below.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/facebook-add-platform.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=7ee5741660e7e45cb2a7c81514e1fd1f" alt="Facebook App Console add platform" width="1077" height="742" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/facebook-add-platform.png" />

* Then select **Android** from the list of available platforms.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/select-android-platform.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=eac3fadcf67a0ce0c6abc1181dba3781" alt="Facebook select Android platform" width="709" height="453" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/select-android-platform.png" />

A panel with platform-specific settings will appear.

* Fill in your **Package ID**.
* Select **Save** (the **Save** button is hidden in the bottom right corner of the page).

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/android-platform-settings.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=b6ffafffedb54020a4a0ad1d51ac078b" alt="Android platform settings" width="821" height="383" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/android-platform-settings.png" />

Build and run the application normally on your Android Device.

* The application should welcome you with a Facebook sign-in page.
* Once you sign in, watch the message on the screen as it changes.
* Finally, you should be presented with a message indicating successful authentication in PlayFab. The entire procedure is illustrated in the example provided below.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/playfab-facebook-auth-on-android.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=73ca707fbe085b61bbeaf369702b2386" alt="PlayFab Facebook authentication on Android" width="1285" height="914" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/playfab-facebook-auth-on-android.png" />

### Running the application on iOS

Before launching the Application on iOS, you have to perform additional configuration steps.

* First, ensure that you have the **iOS** platform selected.
* Then, set a unique **Bundle ID** as indicated in the example provided below.
* Make sure to create your own *unique* **Package ID**.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/set-ios-bundle-id.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=3599c6de938ed564cc5dc7ae572ea8c8" alt="Set iOS bundle ID" width="1061" height="616" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/set-ios-bundle-id.png" />

Next, in your **Facebook Application Console**:

1. Navigate to **Basic** settings.
2. Select **+ Add Platform**, as indicated in the example provided below.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/facebook-add-platform.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=7ee5741660e7e45cb2a7c81514e1fd1f" alt="Facebook App Console add platform" width="1077" height="742" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/facebook-add-platform.png" />

* Select **iOS** from the list of available platforms.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/select-ios-platform.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=396c5464cb9dba1c7da77ec8484bc86e" alt="Facebook select iOS platform" width="709" height="453" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/select-ios-platform.png" />

A panel with platform-specific settings will appear.

* Fill in your **Package ID**.
* Select the **Save** button (the **Save** button is hidden in the bottom right corner of the page).

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/ios-platform-settings.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=14c5ccc92e77b857aa2bddf50a6437dd" alt="iOS platform settings" width="906" height="430" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/ios-platform-settings.png" />

* Next, build the application as usual.
* Once this is done, open the **Project** using **XCode**.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/open-ios-project.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=8b1b51af9c293e12e99de0f37348575a" alt="Open iOS project using XCode" width="770" height="804" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/open-ios-project.png" />

* Select the **Project** to open **Project Settings**.
* Verify that the **Bundle ID** matches your unique **Package ID**.

<img src="https://mintcdn.com/microsoft-4404708b/wQJwYr9vT-FdsCSx/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/ios-project-settings-verify-bundle-id.webp?fit=max&auto=format&n=wQJwYr9vT-FdsCSx&q=85&s=134ffb82cfab08931fdafae6201a9670" alt="iOS project settings: verify bundle ID" width="1946" height="1304" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/ios-project-settings-verify-bundle-id.webp" />

* Make sure to select the correct device.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/ios-project-settings-select-device.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=ba4e9bc8666986003c926585554c88e1" alt="iOS project settings: select device" width="684" height="268" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/ios-project-settings-select-device.png" />

* And select the correct **Identity Team**.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/ios-project-settings-select-identity-team.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=3875ebcc3d1bdd8c50810b9a94c0ed83" alt="iOS project settings: select Identity Team" width="490" height="268" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/ios-project-settings-select-identity-team.png" />

Finally, run the application as normal.

* Once started, the application will allow you to log in to Facebook using the web browser.
* Once done, PlayFab authentication will be invoked, and a message will indicate the result.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/playfab-facebook-auth-on-ios.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=9ec6a4d3c32ebcad5452e897f98c211e" alt="PlayFab Facebook authentication on iOS" width="540" height="720" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/playfab-facebook-auth-on-ios.png" />

### Checking through PlayFab Game Manager

Open your PlayFab title in Game Manager.

* Navigate to the dashboard.
* Refer to the **PlayStream Debugger** panel to check for the latest events. You should see the Facebook authentication event as displayed in the following example.

<img src="https://mintcdn.com/microsoft-4404708b/mLCHf0iQv3VidfBe/images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/playfab-facebook-auth-event.png?fit=max&auto=format&n=mLCHf0iQv3VidfBe&q=85&s=f9d6980c6cef4ca74b9747a5d55d3c45" alt="PlayStream Debugger Facebook authentication event" width="868" height="258" data-path="images/playfab/identity/player-identity/platform-specific-authentication/tutorials/facebook-unity/playfab-facebook-auth-event.png" />

At this point, you have successfully integrated Facebook Authentication for PlayFab.


## Related topics

- [Platform-Specific Authentication](/services/playfab/identity/player-identity/platform-specific-authentication/index.md)
- [Setting up PlayFab authentication using Facebook and HTML5](/services/playfab/identity/player-identity/platform-specific-authentication/facebook-html5.md)
- [Setting up PlayFab authentication using Kongregate and Unity](/services/playfab/identity/player-identity/platform-specific-authentication/kongregate-unity.md)
- [Setting up PlayFab authentication using Steam and Unity](/services/playfab/identity/player-identity/platform-specific-authentication/steam-unity.md)
- [Setting up PlayFab authentication using Google and HTML5](/services/playfab/identity/player-identity/platform-specific-authentication/google-html5.md)
