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.
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.
Another article about java facebook api
http://programmaremobile.blogspot.com/2009/01/facebook-java-apieng.html
Using FacebookXmlRestClient the servlet example prints all logged user info
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
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:
http://www.softwaredeveloper.com/features/develop-facebook-app-072607/
Hi Larre,
THanks alot for the information, The link you provided looks like a great location.
Thanks, Anu
I research Facebook Applications information your blog best But more information for Facebook Applications then see the websit:
Hi Larre,
I have been developing an app for facebook at
http://apps.facebook.com/dotsonline/
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());
}
-jav
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 http://developer.facebook.com/
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.)
Thanks,
David Woosley
@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)
(http://developers.facebook.com/news.php?blog=1&story=106)
On this page you can read about alternatives when using Java:
http://wiki.developers.facebook.com/index.php/Java
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?
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
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
it’s one of the article on the net i found very helpful. i was looking for facebook application building help.
I really appreciate you for providing an interesting and very helpful article. Thanks
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
Great code
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.
[...] englischsprachige Artikel über Facebook Connect mit Java Building a Facebook Application in 15 minutes Logging in with Facebook Java API Facebook authentication in [...]
nice tutorial. it’s very usefull for beginner like me.
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).
thanks a lot…