Skip to main content
Version: 5.11.8

Using Facebook SDK

Flow of the Process
  1. Login via Facebook
  2. Obtain Facebook Access token
  3. Obtain a BACKND Access token by verifying the Facebook Access token
  4. Log in to the BACKND server using the BACKND Access token 5.Use the Facebook Access token to call Backend.BMember.AuthorizeFederation("accessToken", FederationType.FaceBook)

Initial settings

  • Register the app information on the Facebook developer page.
    Facebook Developer Page > Start at the top right
  • Get the app ID and the app secret code and register them to the BACKND Console (Server Settings > Authentication Information).
  • Download Facebook SDK and import it into Unity project
    Facebook SDK for Unity
  • Register Facebook App ID to Unity project
    Top menu > Facebook > Edit Settings > Register App ID
  • Register app-related information to Facebook page
    When Package Name, Class Name, Debug Android Key Hash appear on the top menu in Unity > Facebook > Edit Settings, register them to Facebook Developer Page > Corresponding App > Settings > Basic Settings > Add Platform > Android.

All settings required to obtain the ID token are finished.
You can receive an ID token through the Facebook ID registered in Facebook App Settings.
Additionally, you can add testers via Facebook Developer Console > corresponding app > Settings > Role > Tester > Add Tester.
Please refer to the Facebook Test User document for more details.

3. Facebook console access level settings

For Facebook federation login, you need the appropriate public_profile and email access permissions.
Refer to the Facebook developer documentation below to upgrade to the advanced access permissions for public_profile and email.

4. Facebook console mode settings

You can change the mode of the app in the Facebook console.
Each mode has the following differences.

Mode TypeTest Account and Developer AccountGeneral Account
Developer modeBACKND federation authentication is possible regardless of access level.Without advanced access level permissions, BACKND federation authentication is not possible.
Live modeWithout advanced access level permissions, BACKND federation authentication is not possible.Without advanced access level permissions, BACKND Federation authentication is not possible.

Regardless of the mode, Facebook account login is possible for all developer accounts, general accounts, and test accounts through Facebook SDK; however, federation authentication will not be possible without advanced access level permissions, as authentication will fail during the authentication process between the BACKND server and the Facebook server except for test accounts and developer accounts.

Refer to the following Facebook developer documentation for the types of Facebook modes.


AuthorizeFederation

public BackendReturnObject AuthorizeFederation(string federationToken, FederationType type); public BackendReturnObject AuthorizeFederation(string federationToken, FederationType type, string ect);

Parameters

ValueTypeDescription
federationTokenstringToken value created via each login plugin
federationTypeFederationTypeTypes of federations (FederationType.FaceBook)
ectstring(Optional) The piece of information desired to be saved from the additional information

Description

An attempt is made to sign up or log in using the Facebook member information token value.

After logging in with Facebook, sign-up to the game becomes available through the BACKND server using an ID token. If var perms = new List<string>() { "email" } is included in the Facebook permission settings, the member's email can be viewed in BACKND Console > User Management.

Example

// Include Facebook namespace
using Facebook.Unity;

// Awake function from Unity's MonoBehavior
void Awake ()
{
if (!FB.IsInitialized) {
// Initialize the Facebook SDK
FB.Init(InitCallback, OnHideUnity);
} else {
// Already initialized, signal an app activation App Event
FB.ActivateApp();
}
}

private void InitCallback ()
{
if (FB.IsInitialized) {
// Signal an app activation App Event
FB.ActivateApp();
// Continue with Facebook SDK
// ...
} else {
Debug.Log("Failed to Initialize the Facebook SDK");
}
}

private void OnHideUnity ( bool isGameShown )
{
if (!isGameShown) {
// Pause the game - we will need to hide
Time.timeScale = 0;
} else {
// Resume the game - we're getting focus again
Time.timeScale = 1;
}
}

public void facebookLogin()
{
// Set permission to read
// You can view the member's email information in BACKND Console > User Management.
var perms = new List<string>() { "public_profile", "email" };
FB.LogInWithReadPermissions(perms, AuthCallback);
}

private void AuthCallback ( ILoginResult result )
{
//Login successful
if (FB.IsLoggedIn) {
// AccessToken class will have session details
var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
// Print current access token's User IDDebug.Log(aToken);
string facebookToken = aToken.TokenString;
// Request sign-up with the obtained Facebook token to the BACKND server
Backend.BMember.AuthorizeFederation(facebookToken, FederationType.Facebook);

} else {
Debug.Log("User cancelled login");
}
}

ReturnCase

Success cases

When login is successful
statusCode : 200

When signing up as a new member is successful
statusCode : 201

Error cases

When the account is blocked
statusCode : 403
errorCode : Reason for blocking entered in the console

When the Facebook information entered in BACKND Console is invalid
statusCode : 401
errorCode : BadUnauthorizedException