fb4j Quickstart

This page describes how to quickly set up and use fb4j in your Facebook canvas (FBML or IFRAME) application. It assumes you have created your application using the Facebook Developers Application as described on the Facebook Developers Wiki and have a valid api_key and secret_key.

This guide is valid only for canvas applications (i.e. applications to be integrated into the Facebook canvas). Desktop and external web application quick-start guides are planned for future releases.

1. Download and Install fb4j

Download the fb4j jar from the SourceForge web-site and place it in your classpath (e.g.. copy it into WEB-INF/lib).

2. Set up the Servlet Filter

Configure the fb4j canvas filter in your application's WEB-INF/web.xml by adding the following:

<filter-name>facebookFilter</filter-name>
<filter-class>net.sf.fb4j.canvas.CanvasFilter</filter-class>
<init-param>
  <param-name>apiKey</param-name>
  <param-value>your api_key</param-value>
</init-param>
<init-param>
  <param-name>secretKey</param-name>
  <param-value>your secret_key</param-value>
</init-param>
<init-param>
  <param-name>appBaseUri</param-name>
  <param-value>/</param-value>
</init-param>
<init-param>
  <param-name>requestAttribute</param-name>
  <param-value>canvasRequest</param-value>
</init-param>

3. CanvasRequest and FacebookSession

CanvasRequest wraps useful information contained in the incoming HTTP request, such as the logged in user's id, friend id's and other info, while FacebookSession provides the entry point to Facebook's REST interface functionality. The canvas filter ensures that the user is logged in, has added the application, and that valid Facebook session exists. Both the Facebook session and canvas request wrapper are passed to the application through the HttpServletRequest's attributes. They may be obtained as follows:

CanvasRequest canvasRequest = (CanvasRequest) request.getAttribute( "requestAttribute" );
FacebookSession fbSession = (FacebookSession) canvasRequest.getFacebookSession();

4. Using the REST Interface

Once an instance of the FacebookSession has been obtained, you may query Facebook's REST interface through the corresponding FacebookSession method, such as:

UserInfo userInfo = fbSession.getUserInfo( UserInfo.Field.values() ); 

For a complete list of available methods. see the FacebookSession Javadoc .

5. Presenting the Data

Presently, fb4j provides two ways to access the returned data. Programatically in Java (or in JSP Scriplets), properties are accessed by their bean accessor methods (getters) like so:

Name: <%=userInfo.getFirstName()%> <%=userInfo.getLastName()%>

In JSP Expression Language, properties may be accessed using their default Facebook REST Inteface names:

<h2>Welcome, ${user.first_name} ${user.last_name}!</h2>
<img src="${user.pic_big}"/><br/>picBig<br/><br/>
<img src="${user.pic}"/><br/>pic<br/><br/>
<img src="${user.pic_square}"/><br/>picSquare<br/><br/>
<img src="${user.pic_small}"/><br/>picSmall

(assuming ${user} is an instance of UserInfo in request scope)