Facebook Login API allows users to register or sign in to your app with their Facebook identity.
API uses OAuth 2.0 to handle authentication and authorization. Your app can request access to the user's data via the various authorization scopes provided by the API.
Before you begin to implement Facebook Login with the JavaScript SDK, you need a Facebook App ID, which you can create and retrieve in the App Dashboard. Now you should place it into 'YOUR APP ID' and version 'YOUR API VERSION' .
...
FB.init({
appId : 'YOUR APP ID',
cookie : true,
xfbml : true,
version : 'YOUR API VERSION'
});
...
right now we have option of authorizing users through the OAuth protocol.
Functionalities :
user authorization from Facebook
downloading Facebook user information
The attachment contains an example of use, library and instruction in install folder.
A simple example of using the Facebook Login API.
// Check the status of the user's connection with Facebook.
// If the user is connected, he is spreading the transfer of the legitimate access to the user.
function statusChangeCallback(response) {
if (response.status === 'connected') {
jq.ajax({
type: "POST",
url: '$page.url("api.facebook.ApiFbController","&requestHandler=loggingInFacebook")',
data: "&accessToken="+response.authResponse.accessToken,
dataType: "json",
success: function(resp){
if(resp.data.res==1){
alert('Jesteś użytkownikiem już zarejestrowanym.');
window.location.href=resp.data.link
}else if (resp.data.res==0){
alert('Jesteś nowym użytkownikiem.');
window.location.href=resp.data.link
}else{
alert('Email jest wymagany.');
}
},
error: function(error){
alert("jq.ajax ERROR");
}
});
}
}
// Checking function whether after logging in you have successfully connected to facebook.
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
// Set up a Facebook SDK connection.
// AppID and versions are obtained when creating an account on the Facebook development website.
// AppID is required and individual for each application that has login via Facebook.
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR APP ID',
cookie : true,
xfbml : true,
version : 'YOUR API VERSION'
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
// Load the Facebook SDK.
(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'));
// Change the onclick function: if the user is logged in, the log button will be replaced with the logout link.
function getFbUserData(){
document.getElementById('fbLink').setAttribute("onclick","fbLogout()");
document.getElementById('fbLink').innerHTML = 'Logout from Facebook';
}
// Change onclick function: if the user has been logged out then the logout link is replaced with the log button.
function fbLogout() {
FB.logout(function() {
document.getElementById('fbLink').setAttribute("onclick","fbLogin()");
});
}
package api.facebook;
import palio.*;
import palio.modules.*;
import jpalio.mvc.*;
import jpalio.mvc.annotations.*;
import api.facebook.dao.*;
import universal.model.*;
import palio.pelements.PUser;
import palio.pelements.PSession;
import api.facebook.restFb.UserProfile;
import api.facebook.restFb.GetUserDetails;
public class ApiFbController extends Controller {
private static final User user = Groovy.module("user")
private static final Page page = Groovy.module("page")
// Facebook login function
@RequestHandler(view=["portal.view.accounting.mvc.view.jsonResponse"])
public Object[] loggingInFacebook (@RequestParam(name="accessToken") String accessToken){
// Creates a user logon profile.
GetUserDetails objGetUserDetails = new GetUserDetails();
// Creates an object and based on the access token collects the user's data to the object.
UserProfile objUserProfile = objGetUserDetails.getProfileInfo(accessToken);
// Retrieve the email of the previously created user for subsequent instructions.
String email = objUserProfile.getEmail();
Map mapa = [:] as Map;
try{
// Checks whether the user has agreed to the e-mail. If it does not return from "3".
if(email){
// Checks whether the user is already registered. If so, it gets its data (ref "1") or imports it to BD (ref "0").
if(ApiFbDao.checkingIfUserExists(email)){
ApiFbDao.getUserDataFromDB(email);
mapa["res"] = 1;
}else {
ApiFbDao.setFbDataToBase (objUserProfile.getFirstName(),objUserProfile.getLastName(), objUserProfile.getEmail());
mapa["res"] = 0;
}
}else{
mapa["res"] = 3;
}
// Creates a new session and returns its sessionId.
Long sessionId = creatingNewSesion(email)
// Przekazanie url do skryptu przez Json i przekierowanie strony przez parametr "pageCode".
// Paramtr "pageCode" ustawiany w pliku konfiguracyjnym ("api.facebook.config.Configuration").
mapa['link'] = page.url(api.facebook.config.Configuration.getParam("pageCode"))
return [new JsonResponse(
correct : true,
info : 'Json : Pobrano dane',
data : mapa as Map
)] as Object[]
}catch(Exception e){
return [new JsonResponse(
correct : false,
info : e.getMessage()
)] as Object[]
}
}
private Long creatingNewSesion ( String login){
Long sessionId = user.createSession(login)
PSession session = Instance.getCurrent().getInstance().getSession(sessionId)
// Pass the url to the script via Json and redirect the page via the "pageCode" parameter.
// Paramtr "pageCode" set in the configuration file ("api.facebook.config.Configuration").
Groovy.object(api.facebook.config.Configuration.getParam("setParamObject"),sessionId)
return sessionId
}
@RequestHandler(defaultHandler = true, view="api.facebook.ApiFbLogin")
public Object[] defaultHandler(){
return null
}
public void handleException(Exception ex){
palio.Groovy.object("portal.view.accounting.mvc.view.error", ex)
}
}