Get Facebook profile detail using restfb Graph API in Java & ADF

Sharing is Caring

Facebook is most popular social networking sites now a days and influencing our life in all aspect. Facebook gives us option to use it’s Graph API to access it’s features programmatically, The Graph API is primary way to read and write to facebook social graph using Access Tokens

In this post I am talking about how to access Facebook profile detail using restfb graph API . So First we need to know about Acsess Tokens


What docs says-

When someone connects with an app using Facebook Login, the app will be able to obtain an access token which provides temporary, secure access to Facebook APIs.

An access token is an opaque string that identifies a user, app, or Page and can be used by the app to make graph API calls

The token includes information about when the token will expire and which app generated the token. Because of privacy checks, the majority of API calls on Facebook need to include an access token.

restfb Graph API-

RestFB is a simple and flexible Facebook Graph API client written in Java.It is open source software and simplifies code for you , It provides default implementation for all components . We need to access it’s methods and customized it

Let’s start with getting access token for our application
Open Graph API Explorer , You’ll see a input text there to enter token

 

As we don’t have any token so we need to generate one , click on Get token – Get User Access Token, It’ll open a dialog asking permissions
See here I have requested for following permissions to get user profile

Click on Get Access Token button and allow facebook to give these permissions and continue

It’ll generate a temporary Access Token, that’ll be used to connect facebook

Now created a fusion web application , added two input text to enter AccessToken and Id and a button to submit data

Download restfb jar file and add it to view controller project

Created component binding for both input text and output text (to show response) in managed bean, See managed bean code

Packages Used-

 

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

import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.output.RichOutputFormatted;

 

Component Binding to get and set page data-

 

    //Component Binding for ID field
    private RichInputText idBind;
    //Component binding for AccessToken field
    private RichInputText fbTokenBind;
    //Component Binding for response output text
    private RichOutputFormatted reponseBind;

public void setIdBind(RichInputText idBind) {
this.idBind = idBind;
}

public RichInputText getIdBind() {
return idBind;
}

public void setFbTokenBind(RichInputText fbTokenBind) {
this.fbTokenBind = fbTokenBind;
}

public RichInputText getFbTokenBind() {
return fbTokenBind;
}

public void setReponseBind(RichOutputFormatted reponseBind) {
this.reponseBind = reponseBind;
}

public RichOutputFormatted getReponseBind() {
return reponseBind;
}

 

Method to connect Facebook and access profile detail-

 

    /**Method to connect facebook and access profile details
     * @param actionEvent
     */
    public void fetchInfo(ActionEvent actionEvent) {
        //Get Access Token
        String accessToken = fbTokenBind.getValue().toString();
        //Get Id
        String id = idBind.getValue().toString();
        //Initiate facebook client for accessToken
        fbClient = new DefaultFacebookClient(accessToken);
        //Get User
        User user = fbClient.fetchObject("me", User.class);
        //Get Profile details (We can get only permissible object) using User Object
        reponseBind.setValue("Name- " + user.getName() + "<br>Gender- " + user.getGender() + "<br>DOB- " +
                             user.getBirthday() + " <br>Bio- " + user.getBio() + "<br>Status- " +
                             user.getRelationshipStatus() + "<br>Religion- " + user.getReligion() + "<br>Websites- " +
                             user.getWebsite() + "<br> Hometown- " + user.getHometownName());

}

Now run application and enter UserId and AccessToken that we generated earlier, Click on Fetch button and profile details is on page 🙂

 

Sample ADF Application (Jdeveloper 12.1.3)- Download
Cheers 🙂 Happy Learning

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

2 thoughts on “Get Facebook profile detail using restfb Graph API in Java & ADF”

  1. thanks awasthi..
    If any one is getting error related to SSLKeyException, during running application in local machine, open your console, go to server-> default server -> SSL – > Advance – > Hostname Verification: set it to None. and save it. then run your application

Leave a Reply

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