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 restfb.com 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
index.jsp


<!DOCTYPE html>
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
</head>
<body>
<script>
 // This is called with the results from from FB.getLoginStatus().
 function statusChangeCallback(response) {
 console.log('statusChangeCallback');
 console.log(response);
 console.log(response.authResponse.accessToken);
 alert(response.authResponse.accessToken);
 if (response.status === 'connected') {
 window.location.href='Sign_in_Controller.jsp?access_token='+response.authResponse.accessToken; 
 } 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) {
 statusChangeCallback(response);
 });
 }
 window.fbAsyncInit = function() {
 FB.init({
 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) {
 statusChangeCallback(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); js.id = id;
 js.src = "https://connect.facebook.net/en_US/sdk.js";
 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.
</script>
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
<div id="status">
</div>
</body>
</html>

5.Sign_in_Controller.jsp


<%@page import="rest_fb.User_Profile"%>
<%@page import="rest_fb.Get_User_Details"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <%
 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() %>
</body>
</html>

6. Get_User_Details.java


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());
		obj_User_Profile.setUser_name(user.getName());
		return obj_User_Profile;
	}
}

7. User_Profile.java


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.
http://localhost:8080/Facebook_Login/index.jsp

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.

Facebook_Login.rar

Comments

Leave a Comment