Playing with FACEBOOK Java API

I have some ideas about what I want to do with some nice graphs and it would be more interesting, when it represents some social behavior. Even more interesting when I collect real data from somewhere.. and voila, there is Facebook.

Initially I was eager to write my own implementation, it has REST interface (and I have Jersey client) and it can communicate via XML (and I have JAXB and Jersey supports it and schema is freely available at http://api.facebook.com/1.0/facebook.xsd). But it was harder than I expected and I didn’t find some document which would tell me what I need to send, how to authenticate etc, so I began googling and found “ready & tested” facebook java api: facebook-java-api (version 2.1.1). Well, looks fine but as many other projects there is annoying lack of simple samples or howtos..

So I started googling again and found exactly what I needed: http://straylink.wordpress.com/2008/06/03/logging-in-with-facebook-java-api/, but Mitch used some old version and its not compilable anymore. No biggie, I was able to get it working.. (but I had to checkout sources because there isn’t setIsDesktop method in client and javadoc does not mention anything related; people really should pay more attention to creating proper documentation).

I created my own facebook app (http://www.facebook.com/developers/) and I got a application key and application secret. Important thing to mention – I wanted this to be desktop application and if you’re gonna follow my steps, you have to set it in application settings.

First thing I need for creating “my” graph is get my friends.. this was relatively easy:

FacebookJaxbRestClient client = new FacebookJaxbRestClient(API_KEY, APP_SECRET);

String token = client.auth_createToken();

String url = "http://www.facebook.com/login.php?api_key=" + API_KEY + "&v=1.0" + "&auth_token=" + token;

// Open an external browser to login to your application
Runtime.getRuntime().exec("open " + url); // OS X only!
// Wait until the login process is completed
System.out.println("Use browser to login then press return");

Thread.sleep(2000); // I'm already logged in so just wait until browser
                    // opens a window and continue..
// System.in.read();

String session = client.auth_getSession(token, true);

// session secret should end with "__"; it means that app is in desktop mode
String tempSecret = client.getSessionSecret();

// create new client; you have to use ctr with temp secret, to set desktop mode
client = new FacebookJaxbRestClient(FACEBOOK_URL, API_KEY, tempSecret, session);

// lets get my friends, their names and print it
FriendsGetResponse fgr = client.friends_get();

UsersGetInfoResponse ugi = (UsersGetInfoResponse) client.users_getInfo(fgr.getUid(), EnumSet.of(ProfileField.NAME));

for (User user : ugi.getUser()) {
    System.out.println(user.getUid() + ":" + user.getName());
}

// not allowed to get friends frieds? wtf? :(
// result: com.google.code.facebookapi.FacebookException:
//   Desktop applications cannot call this function for other users
fgr = (FriendsGetResponse) client.friends_get(ugi.getUser().get(0).getUid());

And as you see at the end – you can’t get friend’s friends in desktop app. This is little disappointing.. well, I’ll have to try doing this as a web app next time, at least I finally test PHP support in Netbeans ;-)

Stay tuned!

This entry was posted in java and tagged , , . Bookmark the permalink.

One Response to Playing with FACEBOOK Java API

  1. Pingback: Playing with FACEBOOK PHP API – Just another WordPress instance

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>