Developing Social Login Using Rest Fb

This is an implementation tutorial on how we can use REST FB to enable facebook social login on our web application. Basically, it'...

This is an implementation tutorial on how we can use REST FB to enable facebook social login on our web application.

Basically, it's a project created from javaee7-war template.

To run this app you need to set up a Facebook application with callback url=/oath_callback

pom.xml - we need to define the rest fb dependency which is a java library for logging in facebook.


Callback Servlet -
package com.broodcamp.restfb.servlet;


import javax.inject.Inject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.broodcamp.restfb.provider.FacebookProvider;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.Version;
import com.restfb.types.User;

public class OauthCallbackServlet extends HttpServlet {

 private static final long serialVersionUID = 4400146595698418400L;

 private static Logger log = LoggerFactory.getLogger(OauthCallbackServlet.class);

 private FacebookProvider facebookProvider;

 private String code;

 public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  code = req.getParameter("code");
  if (code == null || code.equals("")) {
   throw new RuntimeException("ERROR: Didn't get code parameter in callback.");
  String accessToken = facebookProvider.obtainAccessToken(code);
  FacebookClient facebookClient = new DefaultFacebookClient(accessToken, Version.LATEST);
  User facebookUser = facebookClient.fetchObject("me", User.class, Parameter.with("fields", "email,first_name,last_name,birthday"));
  log.debug("FB User firstName={}, lastName={}, email={}, birthday={}", facebookUser.getFirstName(), facebookUser.getLastName(), facebookUser.getEmail(),

  RequestDispatcher dispatcher = req.getRequestDispatcher("account.jsf?accessToken=" + accessToken);
  dispatcher.forward(req, res);

Facebook Provider - Provider class for initializing the facebook api.
package com.broodcamp.restfb.provider;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.FacebookClient.AccessToken;
import com.restfb.Version;
import com.restfb.scope.FacebookPermissions;
import com.restfb.scope.ScopeBuilder;

public class FacebookProvider {

 private String appId = "xxx";
 private String appSecret = "yyy";
 private String redirectUrl = "http://localhost:8080/restfb-demo/oauth_callback";
 private String loginDialogUrlString;

 private void init() {
  ScopeBuilder scopeBuilder = new ScopeBuilder();
  scopeBuilder = scopeBuilder.addPermission(FacebookPermissions.EMAIL);
  scopeBuilder = scopeBuilder.addPermission(FacebookPermissions.PUBLIC_PROFILE);

  FacebookClient client = new DefaultFacebookClient(Version.LATEST);
  loginDialogUrlString = client.getLoginDialogUrl(appId, redirectUrl, scopeBuilder);

 public String getAuthUrl() {
  return loginDialogUrlString;

 public String obtainAccessToken(String verificationCode) {
  FacebookClient client = new DefaultFacebookClient(Version.LATEST);
  AccessToken accessToken = client.obtainUserAccessToken(appId, appSecret, redirectUrl, verificationCode);

  return accessToken.getAccessToken();

Repository is available at:


java-library 9119627349944384461

Post a Comment Default Comments


Are you looking for freelancers in the Philippines? Get in touch.


If you like what I do, you can support this channel by buying me a coffee. I would be grateful for your contribution! Your donations will help me create more FREE online courses and learning materials for programmers and remote workers.