So you want to write a FaceBook application using Java? You’ve added the Developer application to your FaceBook account and downloaded the Java client library. And now you’re kind of stuck. Where are the tutorials, examples and proper documentation? Frustration turns to anger, which as we know leads to the dark side.
![]() “I find the lack of good Java documentation disturbing.” |
Hopefully someone will right a good Java/FaceBook tutorial soon. Perhaps we will if you’d only send us some nice chocolates — though to be honest we’re still figuring it out ourselves. Meanwhile, here are some tips to help get you started. It’s not a tutorial, but it should set you on the right track.
8 tips for building Java/FaceBook apps
Use Java 5.0 or higher. That’s just general advice for your health and sanity. Because we care.
Ignore the java library example
The example that ships with the java library is for a desktop app. You probably want to write a web application, so you’ll have to change a great deal. For a start you’ll need some form of web-application server. E.g. you might use TomCat. I don’t, but that is a another story and will be told another time. FaceBook web-applications have a slightly strange usage pattern. Most of your pages will be served through FaceBook. The user will request a page from FaceBook, who will then request it’s main contents from your server. Your contents will be adapted before being sent back to the user. Mostly you don’t need to worry about this — it works nicely. But be aware that JavaScript is not allowed That means if you’re using an AJAX platform, it won’t work within FaceBook. If you need AJAX — and FaceBook’s mock-ajax won’t do — then use your FaceBook pages to direct users off of FaceBook onto normal web-pages.The settings for your application in FaceBook
Don’t forget to fill in the settings for your application in the Facebook Developer application. You should be setting:-- The callback URL for your application.
- The application name - this allows you to make pages within FaceBook (i.e. pages which are framed by the FaceBook navbar and can use FBML). Once set, urls such as
http://apps.facebook.com/yourappname/yourpagenamewill generate a request from facebook to your server. - A URL for new users. This is where you get to welcome them to your application and ask them to spread it on.
Using FacebookRestClient
The most important class in the client library isFacebookRestClient. This contains a host of methods which call the FaceBook server and cover most of what you’ll want to do. UnfortunatelyFacebookRestClientisn’t the friendliest of classes.- Almost all the actions require a FacebookRestClient that was constructed with a session key. If the user is logged in, you can get the session key from the CGI variables (look for
FacebookParam.SESSION_KEY.toString()). Otherwise, you need to send them to a login page. Try the following:-
The method// Create a sessian-less FacebookRestClient FacebookRestClient client = new FacebookRestClient(YOUR<em>API</em>KEY, YOUR<em>SECRET</em>KEY); String token = client.auth<em>createToken(); String loginURL = “http://www.facebook.com/login.php?v=1.0&api</em>key=” +YOUR<em>API</em>KEY+”&auth_token=”+token; // Now redirect the user to loginURL… // When they come back, they should have a session keyFacebookRestClient.auth_getSession()is — as far as I can tell — unnecessary. It converts a session-less client into one with a session. I found it easier just to make a new client, pulling the session information from the CGI variables. - Having got a FacebookRestClient with a session key, you can now call the various FaceBook-editing methods that FacebookRestClient provides. These methods make calling FaceBook easy enough. Unfortunately the methods return unprocessed XML Documents, which isn’t terribly helpful. E.g.
friends_get()returns something like this:-
You’ll probably want to wrap the methods you use with code to extract the information. E.g. to actually use<document> <friendsgetresponse> <uid>1</uid> <uid>2</uid> <uid>3</uid> </friendsgetresponse> </document>friends_get(), try this:-
andDocument d = client.friends_get(); NodeList userIDNodes = d.getElementsByTagName("uid"); int fcount = ids.getLength(); List<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); }friendsnow contains the id numbers for the user’s friends.
- Almost all the actions require a FacebookRestClient that was constructed with a session key. If the user is logged in, you can get the session key from the CGI variables (look for
Servlets return page fragments
When making pages within FaceBook, i.e. with urls such ashttp://apps.facebook.com/yourappname/yourpagenameyou must return an HTML/FBML fragment, and not a full HTML page.Learn FBML. This is a set of special FaceBook tags. They provides you with the easiest way to do many things (e.g. display user names and pictures).
Inviting the user’s friends
So you want to do the viral ‘invite your friends’ thing? Facebook recently changed their API (November 2007). The new improved API is based around a special form, fb:request-form. It is described here: http://wiki.developers.facebook.com/index.php/Fb:request-form. The method for this used to beFacebookRestClient.notifications_sendRequest(). This will no longer work — you’ll get an exception.The profile page
To put content on a user’s profile page, useFacebookRestClient.profile_setFBML(). This overrides whatever default profile-page content you specified in the application settings. When you set a user’s profile FBML FaceBook will cache this content. It does not ask you for updates when the profile is viewed. This makes dynamic profile content a bit fiddlier than it would otherwise be. You’ll need to update the affected profiles with fresh calls toprofile_setFBML()in response to events. Fortunately, although you need a logged in client to do anything, that client can set profile content for any of your users.
That’s all
I’m still very much a beginner regarding the FaceBook API. So if you’re an expert, please do comment below.
Best of luck in creating Web2.0 wonderfulness!

Travis Savo wrote:
July 28th, 2007 at 4:27 am