3 Facebook Context - Reference Documentation
Authors: Benoit Hediard
Version: 2.3.0
3 Facebook Context
The plugin can automatically create afacebookContext
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),
facebookContext
property.MyController { FacebookContext facebookContext
…
def index() {
long userId = facebookContext.user.id
…
}
}
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
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)
// 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, usefacebookContext.authenticated
.if (facebookContext.authenticated) {
// User is authenticated
}
// To get facebook user id
facebookContext.user.id
// To get OAuth user token, required for Graph API calls
facebookContext.user.token
// 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
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