no

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.
<dependency>
 <groupId>com.restfb</groupId>
 <artifactId>restfb</artifactId>
 <version>2.9.0</version>
</dependency>

<dependency>
 <groupId>org.jboss.spec.javax.servlet</groupId>
 <artifactId>jboss-servlet-api_3.0_spec</artifactId>
 <version>1.0.2.Final</version>
</dependency>

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

import java.io.IOException;

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;

@WebServlet("/oath_callback")
public class OauthCallbackServlet extends HttpServlet {

 private static final long serialVersionUID = 4400146595698418400L;

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

 @Inject
 private FacebookProvider facebookProvider;

 private String code;

 @Override
 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(),
    facebookUser.getBirthday());

  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;

@Singleton
@Startup
public class FacebookProvider {

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

 @PostConstruct
 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: https://github.com/czetsuya/RESTFB-Demohttps://github.com/czetsuya/RESTFB-Demo

Related

coding 9119627349944384461

Post a Comment Default Comments

item