× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to integrate Facebook API in Java

In this article we will discuss how to make facebook integration with java.

Websites are become an integral part of any business, be it small, medium or big. The web presence is an important aspect to reach the global audience. But only the website is not sufficient to reach the mass audience. For that the social media revolution is very important and it has a cascading effect. Now the social networking sites are also interested to get integrated with other websites as they also need to reach the mass audience. And in the similar way websites need the help of social sites to reach target audience. So, to complement this social sites have exposed their APIs and allows the websites to communicate with them. Facebook provides convenient Graph API to integrate with their site. The Graph API is very powerful and exposes all necessary features required.

The following picture shows the facebook integration with different types of medium like web, mobile, desktop and many other devices.

Facebook integration cycle

Figure 1: Facebook integration cycle

Facebook application model

Now we need to understand the facebook application model to understanding the integration part. The proxy server model used by facebook is the main integration point with the application. Facebook uses REST web services to open its platform to the developers. Developers are free to use APIs to integrate facebook features in their application. And the most important thing is that the developers get the freedom to select any technology to develop their application. So the application development platform can be anything and facebook APIs are there to support it. And the developer should deploy their application in a way so that it can handle huge volume of data everyday.

Working steps in facebook proxy model:

Following are the steps used in facebook proxy model:

  • Your web application will reside in your web/application server and you will register the base URL in facebook account
  • When your application is visited in facebook, it will call your registered URL on your server
  • Now, your application will call necessary facebook APIs to get information
  • Your application will use its own database data and facebook data and render it
  • After this facebook returns your application's output to the user

Steps to create facebook account:

Before we start integrating our application with facebook APIs, we need to register our application (website) in facebook. Once you register your web application in facebook, access will be provided to the APIs. Please follow the steps below to register your application.

  • Create your website which will be using facebook APIs
  • Create facebook account and register your application there. Here it will be the website name.
  • After this facebook will generate a client id and secret key for the registered web application.
  • Use callback URL during registration. Facebook will use this URL to redirect the response message

How to get facebook libraries:

Before we integrate facebook with our java application we need some third party libraries. These libraries will help us communicate with facebook. Different groups of independent java developers have made efficient facebook libraries for integration purpose.

You can check this link/ to get details. These libraries are compatible with Java SE 5 and above.

Download the following JARS:

If you are using Java application server on top of Java SE 5, then you need to download the following JARs

You do not need the above three JARs if you are using Java SE 6 or later.

After downloading these JARs, you need to incorporate them in your application. These JARs will provide you API access for different purpose.

Following sample code shows the integration part.

Listing 1: Sample showing integration of facebook API and java

package mrbool.com;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.Post;
import facebook4j.ResponseList;
import facebook4j.conf.Configuration;
import facebook4j.conf.ConfigurationBuilder;

public class FacebookImpl {
	public static void main(String[] args) throws FacebookException
		// Make the configuration builder
		ConfigurationBuilder confBuilder = new ConfigurationBuilder();

		// Set application id, secret key and access token

        // Set permission
        confBuilder.setOAuthPermissions("email,publish_stream, id, name, first_name, last_name, generic");

        // Create configuration object
        Configuration configuration = confBuilder.build();

        // Create facebook instance
        FacebookFactory ff = new FacebookFactory(configuration);
        Facebook facebook = ff.getInstance();

		try {
				// Get facebook posts
				String results = getFacebookPostes(facebook);
				String responce = stringToJson(results);

				// Create file and write to the file
				File file = new File("C:\\Facebook\\File\\test.txt");
				if (!file.exists())
					FileWriter fw = new FileWriter(file.getAbsoluteFile());
					BufferedWriter bw = new BufferedWriter(fw);
					System.out.println("Writing complete");
			} catch (IOException e) {
			// TODO Auto-generated catch block
	public static String getFacebookPostes(Facebook facebook) throws FacebookException {
		// Get posts for a particular search
		ResponseList<Post> results =  facebook.getPosts("Reebok");
		return results.toString();

	public static String stringToJson(String data)
		// Create JSON object
		JSONObject jsonObject = JSONObject.fromObject(data);
		JSONArray message = (JSONArray) jsonObject.get("message");
		System.out.println("Message : "+message);
		return "Done";


Facebook Markup Language (FBML):

Apart from using facebook REST web service there are another option available to the developers. Facebook Markup Language (FBML) is another powerful offering from facebook community to support application development. Using this markup language developer can easily integrate their application without putting much effort.

Following is a sample code:

Listing 2: Sample code showing FBML

<%@ page language="java"
   contentType="text/html; charset=UTF-8"
<strong>Myfacebook test page/strong>

   <fb:profile-pic uid="logged_in_user"
                   linked="true" /><br>
   <fb:name uid="logged_in_user"
            capitalize="true" />,
   Use the most popular facebook apps

Apart from FBML, there are other utilities available for Ajax like effects. There is facebook query language (FBQL) which can be used for searching. All these can be used together and it will help us to make a complex application as per our requirement.


There are many social networking websites available but facebook is most popular. And we have also seen that there are many different applications built on top of facebook APIs. There are also third party libraries available which can be used by java applications to interact with facebook. So to conclude our discussion, we can say that social media integration is a new dimension in developer's world and we should explore it as much as possible.

Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login