Learning Facebook API

WRITTEN BY CHERMAINE CHEANG

References:

Introduction

Welcome to learning how to use Facebook API guide. In this guide, you will learn how to incorporate popular Facebook features into your own websites. This guide will first focus on implementing Facebook Javascript SDK to integrate direct Facebook login or sign-up, sharing, posting and commenting directly from your webpage. Then, we will learn about using Facebook's Graph API to get data in and out of Facebook without using those tools provided by Facebook SDK. Before we get started, please note that this is an entry level API guide, and it assumes that you have some knowlegde working with HTML, CSS and Javascript.

Let's get started!

Getting Started

In order to use Facebook API on your own webpage, you need to obtain a unique App ID for your application. You can easily accomplished this by following the steps below:

  1. Login into your Facebook
  2. Create a Developer Account
  3. At your dashboard, select "Add a New App"
  4. Enter the information required
    Create New App ID
    Select "Apps for Pages" for category. Once you are done, select "Create App ID".
  5. Now you have an app ID which can be found under "Dashboard"
    Locate App ID
  6. Choose a Platform your app will work on
    Choosing Platform
    For our case, we will be using the "website" platform.
  7. Setup Facebook SDK for JavaScript by pasting the following code directly after the opening body tag of the HTML where you want to use SDK
    Facebook Javascript SDK

For more information visit https://developers.facebook.com/docs/apps/register

**Important notes: You must have a working url or app domain in order for Facebook Javascript to work on your website. That means, your website must be hosted on some server.

Facebook Login

Facebook Login for Apps is a very popular tool that has been incorporated in many applications, pages and softwares. The main reason developers are using these features is that it provides a fast and hassle free way for their users to use their applications without having to remember multiple usernames and passwords. In addition, since almost everyone in this world has a Facebook account, providing users with a familiar login process will promote user friendliness of the application. Hence, we will look at the easiest ways of using this powerful tool in our webpages.

  1. Check Login Status

    FB.getLoginStatus() is a built-in function provided by Facebook Javascript SDK that allows developers to determine current status of a user when they first enter your website. A user's status can be either connected, not_authorized, or unknown. Here is an example code:

    Check Login Status

    This function will return an response object which look like this:

    Object returned from FB.getLoginStatus()
    • Status - shows current user state
      • connected - user is logged into Facebook
      • not_authorized - user is logged into Facebook, but did not give permission for your app
      • unknown - user is not logged into Facebook or logged out of your app
    • authResponse - provide us with information like userID and accessToken. If authResponse is not present, then user is either not logged into Facebook or did not authorized your app.
  2. If the user is not logged into Facebook, you can either provide a login button on your page to allow them to login, or you can have a popup window that will handle the login process.

    a. To add a login button on your web, simply visit this page to configurate your button. Once you are happy with the size of your button and the basic features of your button, click on "Get Code" and you will be provided with the code needed to implement that button in your html. Simply add the code into your HTML where you want the login button to appear. Here is an example of the code:

    Codes for Login Button

    b. To invoke a popup Login Dialog, simply use the function FB.login()

  3. Making API Calls

    Once the user is logged in and authenticated, you can make API calls using FB.api() on behalf of the user from their browser.

  4. Log out

    Log out can be easily handle using FB.logout() function. If you implemented login using a login button, you can set "auto-logout-link" to true, to automatically provide a logout button once users are logged in.

An example of working codes is shown below Example codes for Facebook Login

For more reference, visit https://developers.facebook.com/docs/facebook-login/web#confirm

Social Plugins

Facebook provides an easy way for developers to incorporate their popular plugins (like, share, send, comment, save and follow buttons). All we need to incorporate these plugins on our websites are just to configurate them and add them to our HTML, very much like adding a login button. Facebook Social Plugins site have all the configuration tools we need to implement all these buttons.

Graph API

Graph API is the primary ways for developers to write and read data on Facebook on behalf of their users. Graph API sends HTTP requests to http://graph.facebook.com accomplish many tasks. For example, developers can use the POST method to post the some content on their user's wall. In addition, Graph API can use the GET method to get information like feeds, albums and comments of their current user. In order to use this API, developers need to have an access token from their current user, and this can be easily obtained by having users login to our apps.

Access token is generated when a user logged into Facebook from a source outside of Facebook. This token is what provides developers a temporary and secure access to Facebook APIs. Access token is similar to an App ID in a way that it is unique for each user. However, access token does has an expiration unlike App ID. When an access token expired, developers can ask for a new access token by asking the user to login again.

Getting Access Token

Making Graph API Request

Once we have the user's access token, we can start making Graph API requests using FB.api(). FB.api() is another built-in method in Facebook Javascript SDK. It can accepts upto four parameters as shown below:

FB.api() method

* indicated required field

GET Request

First, we will talk about making GET requests to read data. Data returned from a GET request is an JSON object with number of key-value pairs depending on the data you requested. We can then use these information to accomplish tasks we wish to accomplish. Let's take a look at some examples:

By using GET request, we can obtain many other informations such as user' likes, user's comments, user's albums, user's feed, etc. A complete list can be found here. However, in order to get access to these data, we again need user's permission which requires that our app be reviewed through the process provided by Facebook Login Review Hence, going forward, all Graph API requests will be performed assuming that you have your application reviewed.

POST Request

POST requests allow us to write data on behalf of our users to Facebook. For example, we can create a new post to user's feed, add comments to user's pictures or feeds, send a message through Facebook Messenger, etc. In order to perform these tasks, we need to update user's permission accordingly. Let's take a look at some examples:

Getting it All Together

An working example that will create the resulting post

Final codes part 1 Final codes part 2 Final codes part 3 Final codes part 4

Results:

Results

For more information about Graph API visit https://developers.facebook.com/docs/graph-api.

The End

Thank you so much for using this guide. Hopefully, I was able to provide you with an introduction to Facebook API and all the neccessary information you needed.