Introducing Facebook SDK For JavaScript

Introduction

In the current scenario the social integration in the websites are very general to enhancing the user experience. Social integration like Facebook authentication, display like or comment widgets or posting on the wall. We'll work on the Facebook SDK for JavaScript today that is used to provide the functionality that can be consumed from the client side script to leverage such an integration.

In that context, I am developing an application and you'll learn to implement the Facebook Authentication or retrieving the user's details like name and photos and you can also post on the user's wall.

We'll proceed here with the following procedure:

  • Working with Facebook App
  • Creating the application
  • Facebook Login by App
  • Posting on User's Wall
  • Facebook Logout

Working with Facebook App

At first you need to have the Facebook App Id and App Secret. If you do not have the Facebook App ID or Secret then you must go to the developers.facebook.com and create the app so that you can have the app id and app secret of your Facebook app. You can also refer to Creating Facebook App for help. So, just copy your App Id only.

Creating the Application

Step 1: Create the new application or website.

Step 2: Add the web form and in the body tag modify the div with the following markup:

<div id="fb-root">

    <input type="button" value="Login" onclick="FbApp_Login();" />

</div>

Step 3: Inside the <head> add the following <script> block:

<script>

    window.fbAsyncInit = function () {

        FB.init({

            appId: 'Your App ID Value',

            status: true,

            cookie: true,

            xfbml: true

        });

     };

 

     (function (doc) {

        var script;

        var id = 'facebook-jssdk';

        var ref = doc.getElementsByTagName('script')[0];

        if (doc.getElementById(id)) {

            return;

        }

 

        script = doc.createElement('script');

        script.id = id;

        script.async = true;

        script.src = "//connect.facebook.net/en_US/all.js";

        ref.parentNode.insertBefore(script, ref);

 

    }(document));

</script>

In the code above the init() method is used to start the Facebook SDK and holds the parameters like appid in which you provide the Facebook app id value and status parameter control whether the current login status of a user is retrieved with each page refresh. A cookie indicates whether a cookie is issued for that session and xfbml indicates whether the markup of the social plugins are parsed.

Facebook Login by App

In this section we will create the login method using the following procedure.

Step 1: Now we'll write the login() method with the following code:

function FbApp_Login() {

     FB.login(function (response) {

         if (response.authResponse) {

             // some code here

         }

 

         else {

             alert("Attempt of Login is Failed!");

         }

      }, { scope: 'email,user_photos,publish_actions' });

 

}

The FbApp_Login() calls the login() method of the Facebook SDK internally that accepts the two parameteres, a callback function that indicates the success or failure of the Facebook login process and the permissions needed by the Facebook app.

Step 2: Run the application and copy the URL and paste it into the site URL of the Facebook app as in the following::

SiteUrl of Facebook Application

Step 3: Now run the application again and click on the Login button, the OAuth dialog to the end user open as shown below:

Facebook Credentials

After entering the credentials, you will be asked for authentication for the fb app for the requested permissions.

Facebook App Authentication

Posting on User's Wall

Now you are successfully associated with the Facebook account. If you want to post on a user's wall, it is possible with the Graph API. Start with the following procedure.

Step 1: Add the following markup after the login button markup:

<label id="lable1">Enter the Message</label>

<input type="text" id="Message" />

<input type="button" id="postButton" value="Post Message" onclick="postMessage();" />

Step 2: Add the following code inside the <script> block:

function postMessage() {

     FB.api('/me/feed''post', {

         message: document.getElementById('Message').value

     });

}

In the code above, the path parameter is set to /me/feed, and the second indicates the method to be used and the last third determines the posting content.

Step 3: Now run the application and enter the message to post:

Message to Posting User Wall

Now, I am checking my wall for the post:

Posting on Wall via Facebook_App

Facebook Logout

In this section we'll create the logout method for logging out using the following procedure.

Step 1: Insert the Logout button by applying the following markup:

<input type="button" value="Logout" onclick="FbApp_Logout();" />

Step 2: Now in the script add the following code:

function FbApp_Logout(){

     FB.logout(function () { document.location.reload(); });

}

It simply calls the Facebook SDK logout() method and simply reloads the web page once the users logs out. That's it.

Summary

This article described the Facebook SDK for JavaScript that integrates the Facebook features to the website and web application. You can learn to authenticate the Facebook App using the Facebook SDK and post the message using the Graph API. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all