RestFB: An easy way to integrate with Facebook

6 Jan

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.Facebook Developers Logo

Before jumping to RestFB, what are the alternatives? I used to develop with another Java client, which is called facebook-java-api, available here:

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 –

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 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

After this step fetch the token from the following page:

You can get detailed information from Authentication guide on Facebook documentation at –

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 –

About these ads

7 Responses to “RestFB: An easy way to integrate with Facebook”

  1. juniordeleon February 7, 2011 at 8:46 pm #

    Hello! your tutorial is so good, but i don’t urdestand about the ACESS_TOKEN :s:s:s. this process is manual or automatically for anybody? Please explain me this step because i’m lose!!!!! Thanks men!!!!

    • Cagri Kilit February 8, 2011 at 11:45 am #

      Getting a token is easy.

      Redirect user to this URL like this” + permissions + “&client_id=” + applicationId + “&redirect_uri=” + landingUrl;

      and your landing url will be called with a parameter named “code”.

      Take that and parse this url with simple http get:” + applicationId + “&client_secret=” + secretKey + “&code=” + request.getParameter(“code”) + “&redirect_uri=” + landingUrl

      Read the contents of the url like:

      String token = readUrl(aturl).split(“&”)[0].replaceFirst(“access_token=”, “”);

      private String readUrl(String urlString) throws IOException {
      URL url = new URL(urlString);
      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

      String response = “”;
      String inputLine;

      while ((inputLine = in.readLine()) != null)
      response += inputLine;

      return response;

      Hope this helps.

  2. infinity April 9, 2011 at 8:02 pm #

    Decent tutorial. I am curious about how to integrate this with google app engine. i found some posts in the restfb google group, which did not help to understand me how to do it on my own because i am new to gae. Maybe you could write a tutorial about how to integrate restfb with gae? I would be very thankful about that.

  3. Nandu October 1, 2011 at 4:32 am #

    Hey hi,I found this stuff interesting, But can i get the full source code so that i can have a deeper thick and thin of it….

  4. Matthias Schneider November 16, 2011 at 12:28 pm #

    hi, what I don’t understand about the tutorial is:
    How can I post a message on any user’s feed on FB from
    a native java swing client ? do I have to open a browser for getting the access token?
    And how can I parse the Url then for the token ? I guess this example is based on a servlet and not a swing app right ?


    • Cagri Kilit November 16, 2011 at 12:36 pm #

      Yes this is a servlet based example. the thing is you have to somehow show the page from fb to get permissions.
      As far as i know there are many integrations via mobile clients, so there must be a mobile api, which you can use for bg tasks.

      There used to be anyway when i was working on this.

      • Matthias Schneider November 16, 2011 at 5:21 pm #

        hi, thanks for the answer!
        My problem is that I don’t have a mobile app, I have a desktop java swing app.
        The following article (see link below) describes exactly what I need but my problem is that it seems that “facebookClient.execute” does not exist anymore in the latest version of the restfb library, only “facebookClient.executeBatchQuery” which is different.

        and so the command

        facebookAuthToken = facebookClient.execute(“auth.createToken”, String.class);

        doesn’t work (I posted it on the site, see last comment but I didn’t get an answer).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 123 other followers

%d bloggers like this: