Session Properties

A user's Facebook authorization status is represented by the session items described here. This status may change during calls to FB.Init, FB.LogInWithReadPermissions, FB.LogInWithPublishPermissions, and FB.Logout.

FB Properties

FB.IsInitialized

boolean type - Check whether the Unity SDK has been initialized. false if the SDK has not been initalized. Use FB.Init to initalize the SDK.

FB.IsLoggedIn

boolean type - Check the user's authorization status. false if the user has not logged into Facebook, or hasn't authorized your app; true otherwise. Most often, this will be in the logic that determines whether to show a login control.

FB.GraphApiVersion

boolean type - Controls what version of Graph API the API method will use. This value is only valid for direct api calls made through FB.Api and the graph api version used by the javscript sdk when running on the web. The underlyting Android and iOS SDKs will still be pegged to the graph api version of this release.

AccessToken

A class in the Facebook.Unity namespace containing the access token granted to your app when the user most recently authorized it. This class is used implicitly for any FB-namespaced method that requires an access token.

AccessToken Properties

Name Type Description
CurrentAccessTokenAccessToken

Static variable containing the current access token

TokenStringstring

String representation of the current the access token

ExpirationTimeDateTime

The expiration date for the current access token

LastRefreshDateTime

The date of the last refresh for the current access token

PermissionsIEnumerable<string&gt;

A list of permissions authorized by the current access token

UserIdstring

Facebook ID of the authorized user in the current access token

Refreshing access tokens on Mobile

The Facebook SDK will manage the lifetime of access tokens for you and will handle making sure that tokens are refreshed before they expire.

It may be desireable to manually refresh the current access token granted to the application by the user in order to retrieve up-to-date permissions, and extend the expiration date, if extension is possible. Use FB.Mobile.RefreshCurrentAccessToken to accomplish this.

Read more at Expiration and Extension of Access Tokens.

Example Usage

// one way to get the current user's profile picture into Unity
if(FB.IsLoggedIn) {
  string url = "https" + "://graph.facebook.com/"+ AccessToken.CurrentAccessToken.UserId +"/picture";
  url += "?access_token=" + AccessToken.CurrentAccessToken.TokenString;
  WWW www = new WWW(url);
  yield return www;
  Texture2d profilePic = www.texture;
}

Best Practices

Most of the time, you will not need to use the AccessToken class. For instance, calls to FB.API will automatically use the current user access token, so you don't need to include it in the parameters of the request yourself. Similarly, functions that invoke dialogs, such as FB.ShareLink, automatically detect and handle session state. You may need it if you are using your own I/O methods though, as in the example.

Never transmit an access token over a non-secure channel. It must only be handled over an encrypted link (that is, in requests using the https scheme).

It is possible, though unlikely within any given session, that user's authorization status will change during a play session. Checking FB.IsLoggedIn once at the beginning of the session is not necessarily sufficient to ensure continued access to user profile information; make sure you catch OAuth errors when interacting with profile features via FB.API.

Most often, this will be in the logic that determines whether to show a login control.

if(FB.IsLoggedIn) {
    // Get data from Facebook to personalize the player's experience
} else {
    // Prompt the user to log in, or offer a "guest" experience
}