Accessing Facebook using Java Program.

Here is a simple java application for accessing facebook. This is done by using restfb, which is a  simple and flexible Facebook Graph API and Old REST API client written in Java.

For this we need an access token which we can generate using graph API explorer in facebook developers website.

Download the restfb jar.

Get an access token with necessary permissions.

package com.amal.fb;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;
import com.restfb.types.User;

public class FacebookAppnew {
public static void main(String[] args) {
 System.setProperty("https.proxyHost", "PROXY");
 System.setProperty("https.proxyPort", "PORT");
 FacebookClient fbClient = new DefaultFacebookClient("MY_ACCESS_TOKEN");
 User user = fbClient.fetchObject("me", User.class);
 // Getting the Details from FB
 System.out.println("UserName :"+user.getName());
 System.out.println("Birthday :"+user.getBirthday());
 System.out.println("Bio :"+user.getBio());
 System.out.println("Email :"+user.getEmail());

 //Making a post in Facebook
 FacebookType publishMessageResponse =
 fbClient.publish("me/feed", FacebookType.class,
 Parameter.with("message", "Good Evening"));


Similarly we can post photos, videos etc.

About amalgjose
I am an Electrical Engineer by qualification, now I am working as a Software Architect. I am very much interested in Electrical, Electronics, Mechanical and now in Software fields. I like exploring things in these fields. I love travelling, long drives and music.

4 Responses to Accessing Facebook using Java Program.

  1. Jon Masters says:

    That’s a really useful tip. Thanks Amal 🙂

  2. hello, you can post one method for obtain access token automatically using Post or other method?

  3. Onder says:

    Following lines return null only getname works.

    System.out.println(“Birthday :”+user.getBirthday());
    System.out.println(“Bio :”+user.getBio());
    System.out.println(“Email :”+user.getEmail());

    • amalgjose says:

      This was written before 2 years and was working perfectly at that time. Not sure about the changes happened to the api. One more thing. If security is enabled in the profile, the code will not be able to fetch the data.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: