Creating a FBML & Java Facebook Application
I’m going to demonstrate how to create a simple Facebook Application in JSP.
You will need a Java 5.0 and Tomcat 5.5 environment to run this.
Facebook Setup
1) Add the Facebook Developers Application to your FB account
2) Set up a New Application. I shall name the app “Zetallite!”
Here are some of the stuff you need to fill in…
- Callback URL - http://<externalipaddress>:<port>/<zetallite>
- Canvas Page URL - http://apps.facebook.com/<zetallite>
- Use FBML
- Can app be added to facebook? Yes
- Who can add this app? Tick the checkbox “Users”
- Post-Add URL - http://apps.facebook.com/<zetallite>
- Side Nav URL - http://apps.facebook.com/<zetallite>
The Callback URL must point to your tomcat server’s external IP address in order for FBML to work. E.g “http://localhost:8080/zetallite” will not work as Facebook cannot reach that URL.
The JSP page
For simplicity, I’ve put everything in to one index.jsp file.
<%@page
import=”com.facebook.api.schema.*”
import=”com.facebook.api.*”
import=”java.util.List”
%>< %
String api_key = “put in your API key here…”;
String secret = “put in your Secret key here…”;
String sess_key = request.getParameter(”fb_sig_session_key”);
FacebookRestClient client = new FacebookRestClient(api_key, secret, sess_key);
client.setIsDesktop(false);
//request.getSession().setAttribute(”client”, client);try {
client.friends_get();
FriendsGetResponse friendsResp = (FriendsGetResponse)client.getResponsePOJO();
List<long> friends = friendsResp.getUid();
out.println(”<h1>ID List of Your Friends</h1>”);
out.println(friends);
}
catch (Exception e)
{
out.println(e.toString());
}
%>
…
Download Code
The code above is just a fragment…
Download the Full Code!
Remember to change the API key and Secret Key.
Note that I am using the Facebook Java API at Google Code. The API Documentation for it can be found here.
App Screenshot
To access your app go to http://apps.facebook.com/<zetallite>
As shown below, the app will displays all your friends ids and a FBML multi-friend-selector.








gr8 man!
*gege*
many more contributions to come, i’m sure!!
ss19rulz said this on April 11th, 2008 at 12:42 am
Hey Mr Zetallite, can you do an open source CMS review / comparison? How about Wordpress vs Joomla vs Drupal vs Movable Type vs Customised vs this vs that
nickpan said this on April 12th, 2008 at 1:43 pm
Choosing an open source cms really comes down to your need and personal preference. You can always use http://www.cmsmatrix.org/ to do a ‘Feature’ comparison of most known cms. In my opinion, more doesn’t always mean better, having a few well implemented features beats having many badly implemented features.
pk said this on April 12th, 2008 at 10:43 pm
Iam not able to get the above one , Iam getting the problem at FacebookRestClient .can you help me in this pleaseeee
Madhavi said this on July 8th, 2008 at 4:28 pm
pk@zetallite.com email me details of your problem please.
pk said this on July 9th, 2008 at 12:02 am
Hi,
Thanks for your information, but one thing let me know hoe u can add 2 or more jsp into faceBook as my Callback URL -> http://www.driveway.com/driveway/jsp/. My question is is how faceBook application will come to know which page it has to call first. Let me know where I can give that information. I have login.jsp and invite.jsp…. need to call invite.jsp from login.jsp…
Looking for quick response from you.
Regards,
Nirmala
Nirmala said this on August 11th, 2008 at 3:25 pm
From what i understand you only can have ONE callback URL. That’s the URL that facebook calls upon initial access of your fb app by a user.
so put your callback URL as www.driveway.com/driveway/jsp/login.jsp
after doing some processing on login.jsp you can then redirect the user to invite.jsp or if login.jsp is a html form the form’s action can be invite.jsp.
pk said this on August 11th, 2008 at 3:33 pm
I have following setting with code
callback URL : www.driveway.com/driveway/jsp/testFBML.jsp
Canvas page URL : http://apps.facebook.com/driwayfbml/
testFBML.jsp action code
*****************************
Welcome
Invite Friends
*****************************
after clicking on invite link it is giving page not found error.. let me know where I can modify my code…
Thanks,
Nirmala
Nirmala said this on August 13th, 2008 at 2:09 pm
Nirmala. i would advise against using a specific file as part of your callback URL. For instance instead of http://myserver.com/myURL/myPage.jsp do something like this: http://myserver.com/myURL. You can always pass parameters like: ?showPage=invite and have your application extract the parameter and make the decision (just like PK said) have your application handle the redirect logic. Make sense? If not, let me know, I’ll try to put a sample together for you.
Good luck!
Josh said this on October 24th, 2008 at 9:36 am
Thank for the input Josh.
Btw, in the end, because of all the silly limitations of FB-Java I (re)did my FB project in PHP instead. No support for FB-Java really is bad news for FB-Java developers. It’s fine if you’re doing your own FB app and have the flexibility but if you’re doing it for a client, it’s hard to tell them why you can’t do what everyone (FB php people) else is doing because of language limitations.
pk said this on October 24th, 2008 at 11:30 pm