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.
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:
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 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.
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:
This function will return an response object which look like this:
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:
b. To invoke a popup Login Dialog, simply use the function FB.login()
Once the user is logged in and authenticated, you can make API calls using FB.api() on behalf of the user from their browser.
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
For more reference, visit https://developers.facebook.com/docs/facebook-login/web#confirm
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 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.
If user is not logged into Facebook, have user login using a login button or FB.login. If user is logged into Facebook, but did not authorized our app, have user authorizes our app.
Once user is logged in and authenticated our app, the response object returned from FB.getLoginStatus will contain the access token we need.
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:
* indicated required field
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:
We can get user's public profile by sending a GET request to http://graph.facebook.com using FB.api() as shown below.
The response object returned to console will look like the above. It will contain information we specified in params. When we expanded the object, we will see the details of the object as below
In addition to user's public profile, we can also get user's email and a list of user's friends who also use our app. However, in order to do so, we need to an access token that allow us to access these information. Hence, we need to ask for user's permission before we can access these information. To obtain permission, we can add addition scopes during our call to FB.login(). An example is shown below:
After getting an updated access token, we can proceed to get these information using the FB.api in similar fashion as getting user's public profile.
The result is shown below:
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 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:
To access user's feed, we need to have user's permission for 'user_posts'. To accomplish this, simply invoke FB.login() with user_posts as the scope. The following code will create a new feed on your wall:
Here's the resulting post:
Unfortunately, because my 'example' app has not been reviewed by Facebook, you will not see any post on your wall. However, the sample result is shown above.
Now to add a comment on the feed we just posted, we first need to get the feed's ID. We can do this by sending a GET request to get a list of feeds of our current user like so
In order for the above code to work, we again need to update our access token to include permission for user_posts. Once we have the updated access token, we will get an JSON object with a list of data containing information about our user's news feed. An example is shown below:
We can then get the feed's id we want by storing it to a variable.
Since data is an array and we want the most recent post from our list, we use data[0] to access the feed's id. Once we have the feed's ID, we can add a comment to the post by sending a POST request.
And the results is shown below:
Unfortunately, because my 'example' app has not been reviewed by Facebook, you will not see any post on your wall. However, the sample result is shown above.
We can also add a comment to a comment by sending the same POST request but using comment's id instead of feed's id. We also need to set the parent component of this new comment to the comment's id we are adding the comment. Here's is an example:
Here's the resulting post:
Unfortunately, because my 'example' app has not been reviewed by Facebook, you will not see any post on your wall. However, the sample result is shown above.
An working example that will create the resulting post
Results:
For more information about Graph API visit https://developers.facebook.com/docs/graph-api.
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.