Building a Facebook Application in 15 minutes
Oct 10th, 2007 by larre
So, after fowa and 90k+ developers in mind, I just had to go ahead and implement a Facebook application. Here is the code and how I implemented it.
First of all my goal was to use an existing web application, write a facebook integration module that communicates with the Facebook APIs, extracting some information/data and presenting them inside the existing web application. The web application I used is based on a j2ee framework (jsp, java). The architecture is much like Struts The first thing to do was to get the Developer Application to my existing Facebook account. Then I had to download the Client Library. I choosed the Java Library since my development environment was Java. When I extracted the Client Library, only the source files where included. So what I had to do first (besides looking into the source code), was to compile the java-files. I actually wrote an ant-task for this purpose, but a more easier way is just to use javac without using ant-framework. The next step was to take the output from the compile process and put it into an archived file (.jar). This is not necessary but I like to do it this way and the facebook.jar file I now had is easily dropped into my existing web application library. In my Facebook profile I had to create (set up) a new application. This is done by clicking the developer application that was installed earlier. Its some steps including getting a key (API key and secret key) that I used in my java-code but this process is straight forward.
Ok, now lets start the coding process. Here is the java code controlling the session and authentication:
String apiKey = “something”;
String secretKey = “something”;
FacebookRestClient frc = null;
HttpSession session = request.getSession();
String sessionKey = (String) session.getAttribute(”facebookSession”);
String token = request.getParameter(”auth_token”);
try {
if (sessionKey != null && sessionKey.length() > 0) {
frc = new FacebookRestClient(apiKey, secretKey, sessionKey);
this.doTheThing(request, response, frc);
} else if (token != null) {
frc = new FacebookRestClient(apiKey, secretKey);
session.setAttribute(”facebookSession”, sessionKey);
sessionKey = frc.auth_getSession(token);
session.setAttribute(”facebookSession”, sessionKey);
this.doTheThing(request, response, frc);
} else {
response.sendRedirect(”http://www.facebook.com/login.php?api_key=” + apiKey + “&v=1.0″);
}
} catch (FacebookException fe) {
} catch (IOException ioe) {
…
The doTheThing method is extracting the information and looks like this:
int myid = frc.users_getLoggedInUser();
EnumSet<ProfileField> fields = EnumSet.of(com.facebook.api.ProfileField.NAME, com.facebook.api.ProfileField.PIC, com.facebook.api.ProfileField.PIC_BIG, com.facebook.api.ProfileField.PIC_SMALL);
Collection<Integer> users = new ArrayList();
users.add(myid);
// Get my information
Document d = frc.users_getInfo(users, fields);
String myname = d.getElementsByTagName(”name”).item(0).getTextContent();
String mypicture = d.getElementsByTagName(”pic”).item(0).getTextContent();
// Get my friends id
Document d2 = frc.friends_get();
String s = d2.toString();
NodeList userIDNodes = d2.getElementsByTagName(”uid”);
int fcount = userIDNodes.getLength();
Collection<Integer> friends = new ArrayList<Integer>();
for (int i = 0; i < fcount; i++) {
Node node = userIDNodes.item(i);
String idText = node.getTextContent();
Integer id = Integer.valueOf(idText);
friends.add(id);
}
List l = new ArrayList();
Map m = new HashMap();
Document d3 = frc.users_getInfo(friends, fields);
// Get my friends information
for (int j = 0; j < fcount; j++) {
String name2 = d3.getElementsByTagName(”name”).item(j).getTextContent();
String picture2 = d3.getElementsByTagName(”pic”).item(j).getTextContent();
String picture3 = d3.getElementsByTagName(”pic_small”).item(j).getTextContent();
String picture4 = d3.getElementsByTagName(”pic_big”).item(j).getTextContent();
m.put(”name”, name2);
m.put(”picture”, picture2);
m.put(”picture_small”, picture3);
m.put(”picture_big”, picture4);
l .add(m);
m = new HashMap();
}
request.setAttribute(”friends”, l);
request.setAttribute(”myname”, myname);
request.setAttribute(”mypicture”, mypicture);
request.setAttribute(”numFriends”, new Integer(fcount));
So all the information from Facebook is now added to the request and can be displayed in a JSP. The code above is not very elegant and is not optimized(!). Its just a pice of code done after half an hour of work and my goal was to implement an integration module for testing purpose.
Hope this blog post can help others looking for hints when getting started with Facebook application development/integration.


There are actually several mashup tools out there now that don’t require you to do any coding coding at all…
Here’s an overview: http://ouseful.open.ac.uk/blog/010616.html
tony
Hi Tony, thx for your comment and link!. I always do it the hard way first, thats where the fun is
Larre,
You rock. There is so much confusion on how to use the Java tools that Facebook provides that you find yourself chasing your own tail… until you find your site!
THANK YOU!
Facebook development using Java has been a total nightmare for me. I couldn’t get it to work using a java redirect as used in this example. I had to redirect to a jsp that contained an FBML redirect element. Even then for some reason the page it redirects to doesn’t want to render on the facebook canvas. Sigh.
Thanks Larre, this was very helpful.
FB changed their api so that you cannot instantiate it anymore. one workaround is to replace all instances of FacebookRestClient in your code with FacebookXmlRestClient
Se this comment on:
http://forum.developers.facebook.com/viewtopic.php?pid=12421
Hi,
Thanks for that great information.
I succeed implementing this and I now I authenticate the user + save the authenticated facebook client in the session + return a form to the user.
The user fill the form and submit…
But then, I get a new session id (in my web server) for the form submission request and of course I can not get the already saved authenticated client from the session.
I can do the authenticated process again but I’m sure this is not the way.
What am I doing wrong?
I use Spring and I thought it will handle the session for me, do I need to pass the session id back to the client by myself?
Thanks,
Yaron
Hi,
I am developing a website and a Facebook Application to attract the users to the same.
I want to make sure that if someone already has the application and is signed on to Facebook, then there is no need to enter log-in info if they click on any of the website links on the Facebook Application. What I want is for the site and Facebook to work seamlessly as if they were part of the same site.
Is this possible? How can i do this?
Look forward to your response.
Thanks,
Steve
Yaron and Steve, probably you have got your answers. I am sorry that I am not able to help you out. I have not looked further into FB and the API. Have just implemented this simple integration test. Hope you figured it out!
Does anyone know what would be involved if I were to publish a JPG to a facebook profile from an external website? …like a facebook badge?
Dear Friends,
I am stuck with this. I wanted to create an application using Java(J2EE). I have successfully created the application. But suddenly what happend I do not know…Now Facebook has changed something and the same code which was working is now throwing exception. I have changed The FacebookRestClient to FacebookXmlRestClient…Then too it does not work. Can any one help me regarding this. My main intention is to login , get information of his and his friends, display all friends information and send request to them(Which I had done 3 months back). but now I am no where on the track. Nothing works out for me. Is it a problem of facebook? please help me if any of have the solution.
[...] pm on May 2, 2008 | # | Tags: facebook FaceBook app in 15 minutes, what? no! http://www.larre.com/2007/10/10/building-a-facebook-application-in-15-minutes/ [...]
That’s very helpful!! Thanks a loooooooooooooot!!
hi this is Nagendra,
i am developing a website using java 1.4 can i integrate this website with facebook.
thanks and regards,
nagendra.