(Quick Reference)

3 Facebook Context - Reference Documentation

Authors: Benoit Hediard

Version: 2.3.0

3 Facebook Context

The plugin can automatically create a facebookContext Spring bean, with all the required info:
  • current Facebook signed request (if defined).
  • current Facebook app,
  • current Facebook user (if authenticated),
  • current Facebook page (if running in a facebook page tab),

To automatically inject it in your controllers, add the facebookContext property.

MyController {

FacebookContext facebookContext … def index() { long userId = facebookContext.user.id … } }

Note: facebookContext is a request scope bean and should only be used in Grails controllers (or other request scope beans).

If you want to access facebookContext from a non request scope bean (ex.: singleton filters), you should use facebookContextProxy (AOP scoped proxy).

MyFilter {

FacebookContext facebookContextProxy … def index() { long userId = facebookContextProxy.user.id … } }

Signed Request

The context gives you access to the current signed request sent to your app by Facebook :

  • on apps.facebook.com or in a page tab, through signed_request params posted to the app iframe,
  • on external sites (web or mobile) through Facebook JS SDK signed request cookie.

// To get access to signed request data
facebookContext.signedRequest

But the signed request is automatically parsed and all its data are available in FacebookContext, so you should not have to use it.

If you are running your app on apps.facebook.com or in a page tab, signedRequest is only available on initial request (if you navigate inside the iframe, signed request is lost if you do not pass it to your links params).

Facebook App

The context gives you access to current Facebook app info.

// To get Facebook app id (defined in Config.groovy)
facebookContext.app.id
// To get Facebook app permissions (defined in Config.groovy)
facebookContext.app.permissions
 // To get Facebook app secret (defined in Config.groovy)
facebookContext.app.secret
 // To get default app token, concatenation of id and secret
facebookContext.app.token
// To get OAuth app token, required for Graph API calls (it will generate a Graph API call)
facebookContext.app.getToken(true)

If you are running your app in a page tab, you'll get additional info:

// To get app data pass through page tab query string (...&app_data={SOME_STRING}...)
facebookContext.app.data

Facebook User

To check if current user has authorized your app and is authenticated, use facebookContext.authenticated.

if (facebookContext.authenticated) {
    // User is authenticated
}

If authenticated, the context gives you access to current Facebook user info.

// To get facebook user id
facebookContext.user.id
// To get OAuth user token, required for Graph API calls
facebookContext.user.token

If the app is running on an external site (web or mobile), the access token will be automatically fetched in exchange for the authorization code returned by Facebook JS SDK signed request cookie or server-side redirect query string.

If the app is running on apps.facebook.com or in a page tab, the access token will be extracted from the signed request params (passed through the canvas iFrame) and you'll get additional info:

// To get facebook user age restriction (only returned in Canvas apps, it will not be returned for external apps)
facebookContext.user.age
// To get facebook user country
facebookContext.user.country
 // To get facebook user locale
facebookContext.user.locale

In order to optimize performances, user id and token are automatically stored in session scope.

By default token are only valid during 2 hours, if a request is made with an expired token, session will be automatically invalidated. If required, you can exchange the short-term token for a long-lived one (valid during 60 days).

if (facebookContext.authenticated && !facebookContext.user.tokenExpired) {
  // Exchange token to get an extended expiration time (60 days)
  log.info "Current token expiration time: " + new Date(facebookContext.user.tokenExpirationTime)
  facebookContext.user.exchangeToken()
  log.info "Exchanged token expiration time:  " + new Date(facebookContext.user.tokenExpirationTime)
}

Facebook Page

If the app is running in a page tab, the context gives you access to current Facebook page info.

Otherwise, facebookContext.page is null. You must check if it exists before accessing it.

// To get facebook page id (where the app is installed)
facebookContext.page?.id
// To know if current user has liked the current page
facebookContext.page?.liked
 // To know if current user is an admin of the current page
facebookContext.page?.admin