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's a p...


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

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: Social Login using REST FB
Social Login using REST FB
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfSie7LuHwTPjzf_u0Llr3qd4jXaM-eoDuLzoMf3cK5DmPhmHFZNc0b2CJ78zb6n4wy49R5pp18Oou2D0gN8pTg6aEeXSe5BQTxYLw_80g_4INECMvOGQEa-ewddtwnPnffg7PPgBL5BE/s400/restfb.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfSie7LuHwTPjzf_u0Llr3qd4jXaM-eoDuLzoMf3cK5DmPhmHFZNc0b2CJ78zb6n4wy49R5pp18Oou2D0gN8pTg6aEeXSe5BQTxYLw_80g_4INECMvOGQEa-ewddtwnPnffg7PPgBL5BE/s72-c/restfb.png
toztech
https://toztech.blogspot.com/2018/09/social-login-using-rest-fb.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2018/09/social-login-using-rest-fb.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content