Social Login Using Balance Fb


This is an implementation tutorial on how nosotros tin role REST FB to enable facebook social login on our spider web application.

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

To operate this app you lot require to develop a Facebook application amongst callback url=/oath_callback

pom.xml - nosotros require to define the balance fb dependency which is a coffee library for logging inwards 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") populace class OauthCallbackServlet extends HttpServlet {   someone static lastly long serialVersionUID = 4400146595698418400L;   someone static Logger log = LoggerFactory.getLogger(OauthCallbackServlet.class);   @Inject  someone FacebookProvider facebookProvider;   someone String code;   @Override  populace void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {   code = req.getParameter("code");   if (code == nothing || code.equals("")) {    throw novel RuntimeException("ERROR: Didn't popular off code parameter inwards callback.");   }   String accessToken = facebookProvider.obtainAccessToken(code);   FacebookClient facebookClient = novel 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 populace class FacebookProvider {   someone String appId = "xxx";  someone String appSecret = "yyy";  someone String redirectUrl = "http://localhost:8080/restfb-demo/oauth_callback";  someone String loginDialogUrlString;   @PostConstruct  someone void init() {   ScopeBuilder scopeBuilder = novel ScopeBuilder();   scopeBuilder = scopeBuilder.addPermission(FacebookPermissions.EMAIL);   scopeBuilder = scopeBuilder.addPermission(FacebookPermissions.PUBLIC_PROFILE);    FacebookClient customer = novel DefaultFacebookClient(Version.LATEST);   loginDialogUrlString = client.getLoginDialogUrl(appId, redirectUrl, scopeBuilder);  }   populace String getAuthUrl() {   render loginDialogUrlString;  }   populace String obtainAccessToken(String verificationCode) {   FacebookClient customer = novel DefaultFacebookClient(Version.LATEST);   AccessToken accessToken = client.obtainUserAccessToken(appId, appSecret, redirectUrl, verificationCode);    render accessToken.getAccessToken();  } } 

Repository is available at: https://github.com/czetsuya/RESTFB-Demohttps://github.com/czetsuya/RESTFB-Demo
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.