Building a Facebook Application in 15 minutes

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.

See also: Reflections after having implemented Facebook Connect and Google Friend Connect on my blog

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. [% oiopub-banner-4-right %]

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(“” + 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();

// 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);

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

  41 comments for “Building a Facebook Application in 15 minutes

  1. October 11, 2007 at 7:35 pm

    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:


  2. October 11, 2007 at 8:05 pm

    Hi Tony, thx for your comment and link!. I always do it the hard way first, thats where the fun is 🙂

  3. October 19, 2007 at 2:11 am


    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!


  4. BigE
    November 7, 2007 at 10:56 pm

    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.

  5. November 8, 2007 at 1:01 pm

    Thanks Larre, this was very helpful.

  6. November 18, 2007 at 9:20 pm

    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:

  7. November 29, 2007 at 9:40 am

    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?


  8. December 17, 2007 at 6:34 pm


    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.


  9. January 10, 2008 at 12:21 pm

    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!

  10. January 24, 2008 at 4:36 pm

    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?

  11. February 15, 2008 at 10:35 am

    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.

  12. Joy
    July 31, 2008 at 10:43 pm

    That’s very helpful!! Thanks a loooooooooooooot!!

  13. Nagendra
    September 1, 2008 at 2:36 pm

    hi this is Nagendra,

    i am developing a website using java 1.4 can i integrate this website with facebook.

    thanks and regards,

  14. January 20, 2009 at 3:48 pm

    Another article about java facebook api

    Using FacebookXmlRestClient the servlet example prints all logged user info

  15. February 22, 2009 at 12:47 am

    Hi Larre, I ran into your blog while searching examples or tutorial related to data extraction from facebook using facebook java api. What I really want to do is extract data and load to my own data model for some mining experiments. I followed the example you have provided and I am not sure if this would be a good starting point. I was wondering if you could provide some guidence or point me towards to any tutorials do such task.
    Thank You, Anu

  16. February 22, 2009 at 2:03 am

    Hi Anu,
    I think this post that I did back in 07 is quit outdated. FB did change their API to some extent.

    Maybe this could be to any help:

  17. February 22, 2009 at 7:52 pm

    Hi Larre,
    THanks alot for the information, The link you provided looks like a great location.
    Thanks, Anu

  18. April 12, 2009 at 5:54 pm

    I research Facebook Applications information your blog best But more information for Facebook Applications then see the websit:

  19. jav
    April 20, 2009 at 3:29 pm

    Hi Larre,

    I have been developing an app for facebook at

    Here I am sending update to profile when a user enter the page.

    But my problem is that too many updates are going.

    Is there a way where it can be configured in facebook api

    Here is the code which I am using.

    String str=”is playing a game of Dots and Boxes”;

    if(facebook.users_hasAppPermission(Permission.STATUS_UPDATE,new Long(user).longValue()))
    facebook.users_setStatus(str,false,true,new Long(user).longValue());


  20. April 22, 2009 at 11:44 pm

    Hello jav,
    when i worked with the FaceBook API, I never came over an update limit. Sorry not to be able to help you out other than to point you to the documentation and forums at

  21. David Woosley
    May 4, 2009 at 4:01 pm

    I’m getting the impression that those of us with complex, existing Java-based websites are screwed when it comes to integrating them into Facebook, especially when you consider the fast pace at which Facebook is changing their API rules. All of the blog entries and other articles on using Java with Facebook are negative in tone. Detailed examples of the integration are sparse.

    So, what’s the best medium-term to long-term solution for integrating Java into Facebook. (I know nothing about PHP.)


    David Woosley

  22. May 6, 2009 at 11:29 am

    @David Woosley
    I have the same feeling. It’s easier to work with FB and PHP it seems.

    Back in 2008 FB announced that their support for the Java Client Library was dropped (discontinued)

    On this page you can read about alternatives when using Java:

  23. May 12, 2009 at 4:52 pm

    Larre, why do you think Facebook ditched Java? Is it considered “too difficult” for their target audience of programmers? Or, maybe the Facebook staff won’t commit to the backwards compatibility required by the Java community?

  24. May 18, 2009 at 9:40 pm

    David hmm ,I’m really not sure. But anyway it does not matter that much. Some Java clients are out there, and if they are not good enough it’s kind of straight forward writing one 🙂

  25. July 7, 2009 at 9:27 pm

    I am so glad I found it. It is easy to understand and fun to read.
    Love your writing style and the design of your blog, your blog is great thanks for the info

  26. July 22, 2009 at 11:23 am

    it’s one of the article on the net i found very helpful. i was looking for facebook application building help.

  27. August 3, 2009 at 12:16 pm

    I really appreciate you for providing an interesting and very helpful article. Thanks

  28. August 5, 2009 at 12:33 pm

    Its intresting to know that facebook applications also make in 15 minutes, But it also about quality of applications. Mostly people used facebook app to get entertained. Nice info. Thanks

  29. January 22, 2010 at 8:36 am

    Great code

  30. February 13, 2010 at 6:51 pm

    Thanks for the wonderful piece of information.. loved the simplicity.. have been using Twitter API for sometime and was finding the going difficult with Facebook api compared to Google and Twitter API.

  31. March 6, 2010 at 4:53 am

    nice tutorial. it’s very usefull for beginner like me.

  32. June 8, 2010 at 3:50 pm

    I recommend to use free PHP IDE Codelobster PHP Edition with special Facebook plug-in for Autocomplete for Facebook JavaScript SDK, Facebook PHP SDK, FQL (Facebook Query Language) and FBML (Facebook Markup Language).

  33. shadow_sat
    June 20, 2010 at 1:20 pm

    thanks a lot…

  34. September 1, 2010 at 10:44 am


    I wanted to say that I have been following for a while and I would like to sign up for the feed. I’ll give it a try but I might need some assistance. This is a great find and I would hate to lose touch, and maybe never discover it again.

    Anyway, thanks again and I look forward to reading/posting again sometime!

  35. Temoavaro
    October 2, 2010 at 4:10 am
  36. October 28, 2010 at 1:51 am

    Great, Thanks a lot

  37. November 28, 2010 at 12:47 pm


    I am looking for a VPS solutions. Who is the best provider in your eyes of VPS hosting?


  38. November 28, 2010 at 3:22 pm

    rickgrill, I have not tested many services. I have used DreamHost for several years and are really happy with them!

  39. Mahipal
    May 12, 2011 at 2:25 pm

    I’m successfully created application on faceboo. API & Secret keys are available to me. And i’m able to generate Auth_Token also. But when i call

    I’m getting exception help me resolving this issue. I’m getting struck over this and not able to move further.

    Please help me resolving this issue. I’m getting struck over this and not able to move further.

Leave a Reply

Your email address will not be published. Required fields are marked *