Press enter to see results or esc to cancel.

JAVA- Integrate login with Facebook using restfb API

In this video I have shown how you can integrate Login with facebook in web projects using restfb API.
All the documentations are available in documentation section.

Here is the steps what you have to follow.
1. Login to facebook developers site and get an APP id by creating a facebook APP.

2. Grant permission for your Domain to access the Facebook app. You can go to this link Grant Permission for the steps.
3. Project structure in Eclipse IDE

4. Update the APP ID in the page index.jsp

<!DOCTYPE html>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
 // This is called with the results from from FB.getLoginStatus().
 function statusChangeCallback(response) {
 if (response.status === 'connected') {
 } else {
 // The person is not logged into your app or we are unable to tell.
 document.getElementById('status').innerHTML = 'Please log ' +
 'into this app.';
 // This function is called when someone finishes with the Login
 // Button. See the onlogin handler attached to it in the sample
 // code below.
 function checkLoginState() {
 FB.getLoginStatus(function(response) {
 window.fbAsyncInit = function() {
 appId : 'FILL APP ID HERE',
 cookie : true, // enable cookies to allow the server to access 
 // the session
 xfbml : true, // parse social plugins on this page
 version : 'v2.8' // use graph api version 2.8
 FB.getLoginStatus(function(response) {
 // Load the SDK asynchronously
 (function(d, s, id) {
 var js, fjs = d.getElementsByTagName(s)[0];
 if (d.getElementById(id)) return;
 js = d.createElement(s); = id;
 js.src = "";
 fjs.parentNode.insertBefore(js, fjs);
 }(document, 'script', 'facebook-jssdk'));
 // Here we run a very simple test of the Graph API after login is
 // successful. See statusChangeCallback() for when this call is made.
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
<div id="status">


<%@page import="rest_fb.User_Profile"%>
<%@page import="rest_fb.Get_User_Details"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
 String access_token=(String)request.getParameter("access_token");
 Get_User_Details obj_Get_User_Details=new Get_User_Details();
 User_Profile obj_User_Profile=obj_Get_User_Details.Get_Profile_info(access_token);
 User Name : <%=obj_User_Profile.getUser_name() %>


package rest_fb;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Version;
import com.restfb.types.User;
public class Get_User_Details {
	public User_Profile Get_Profile_info(String accesstoken) {
		User_Profile obj_User_Profile=new User_Profile();
		FacebookClient facebookClient = new DefaultFacebookClient(accesstoken, Version.VERSION_2_6);
		User user = facebookClient.fetchObject("me", User.class);
		System.out.println("User name: " + user.getName());
		return obj_User_Profile;


package rest_fb;
public class User_Profile {
	private String user_name;
	public String getUser_name() {
		return user_name;
	public void setUser_name(String user_name) {
		this.user_name = user_name;

8. Run the project in tomcat server. There will be login button visible.

9. Login to Facebook on the Login pup up.Once you click the login button, It will ask for user permission.
App permission.

10. After successful login, It will redirect to Sign_in_Controller.jsp with active access token in the URL.
Using the active access token Restfb API will get the user information from the logged in Profile and print the name.

11. Download the complete project here.



Leave a Comment