Facebook is leading social media and if you want to create a social project of any kind, Facebook integration is inevitable. In this post I will make an introduction for Java developers to make proper integration to Facebook with RestFB.
Before jumping to RestFB, what are the alternatives? I used to develop with another Java client, which is called facebook-java-api, available here: http://code.google.com/p/facebook-java-api/
I can’t say it is a bad implementation, but the problem with this api is it uses facebook old rest api for everything. In case you don’t know there are 2 apis released by Facebook to Facebook Developers: Old Rest API and Graph API. Facebook clearly says that the future is graph api. So I’d rather stick to that while developing an application. I see that it is much faster and error prone compared to old api. I was experiencing lots of timeouts from the old rest api, but the graph api proved to be really fast. So my suggestion is be open minded and use graph api instead.
RestFB – http://restfb.com/
As I told RestFB is a light weight java implementation of the graph api. But the good thing is it also supports the old rest api. Old rest api is sometimes needed when graph api is not enough (Some features are not yet in Graph APi but coming).
You can access the docs for the api in the link above, the source code is also available.
So lets start with the integration.
1. Create you Facebook app
I will not go into details around here because it is really easy. Go to facebook.com/developers then create an app.
2. Getting a token
To get a token and access graph api features, you need to authenticate your users. From a web application you can easily authenticate using OAuth2.0. When you create your Facebook app you were given an id, key and secret. Redirect the user to
Then facebook will redirect the user to you callback handler specified above with an argument called “code”.
If you want additional permissions, you should add the scope parameter to the call
Full list of permissions are available at http://developers.facebook.com/docs/authentication/permissions
After this step fetch the token from the following page:
You can get detailed information from Authentication guide on Facebook documentation at – http://developers.facebook.com/docs/authentication
3. Getting data
Getting data is really easy after this point. You have an access token, and you can get information behalf of that user now. As an example getting self information:
FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN);
User user = facebookClient.fetchObject(“me”, User.class);
out.println(“User name: ” + user.getName());
or publishing a status message (You need publish_stream permission for this)
FacebookType publishMessageResponse = facebookClient.publish(“me/feed”, FacebookType.class, Parameter.with(“message”, “Hello Facebook!”));
This is it. This was Facebook 101 for beginners. For complicated stuff you can dig into RestFB documentation – http://restfb.com/