For Developers: Facebook's Graph API

In this article, I will be talking about Facebook’s Graph API.
Before diving deep into the API, we first need to understand what an API is.

What is an API

API stands for Application Programmable Interface.
It is the interface that is responsible for the communication between the client and the server, your app and the machine etc. Let me give you a simple example.

Example of an API

Suppose you are at the hostel, and you want to eat a Pizza.
The Pizza is being made in the kitchen, but you do not know how to get that Pizza.
In short, you want to talk to the people in the kitchen, and in-return they would send you the Pizza. For this communication, the person who would connect you and the people in the kitchen is a waiter. Waiter in this case is acting as an API. You send a request to API. API process that request and forward that to the module for which that request was intended to; that module will reply you back through that API.
The communication between two different modules occurs through API.

This is the simple mechanism behind any of the API.

Graph API of Facebook

Now let us talk about Graph API of Facebook! According to
the official documentation of Facebook’s Graph API:

The Graph API is the primary way to get data into and out of the Facebook platform.
It's a low-level HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.

The above overview is self-explanatory.
Next, we need to know the cogs of Graph API.
Following are the basic units that make up the whole Graph API.

Basic Units of Facebook API

Following are the three basic units of Facebook API:

1. Nodes

Nodes are the individual objects, such as a User, a Photo, a Page, or a Comment.

2. Edges

Edges are the connections between a collection of objects and a single object, such as Photos on a Page or Comments on a Photo etc.

3. Fields

Fields contain the data about an object, such as a User's birthday, or a Page's name.

Typically we use nodes to get data about a particular object, and we use edges to get collections of objects on a single object, and we use fields to get data about a single object or each object in a collection.

Requesting the information using GraphAPI and Browser

As I stated above that the Graph API is HTTP-based; therefore, it will work with any language that supports HTTP.
For example, cURL and urllib.
Hence, you can also use Graph API in a browser.

Example

https://graph.facebook.com/youtube?fields=feed

In the above HTTP link, we are actually requesting the feed of YouTube page on Facebook.
The equivalent to this in cURL request is:

curl -i -X GET \  https://graph.facebook.com/youtube?fields=feed
&access_token={valid-access-token-goes-here}

If you go to the above link, you will get the exception that you need a valid access token.
Now the question is how can we get the access token? Let us look at that in depth.

Getting the Access Tokens

Almost all Graph API requests require an access token.
If you are creating an Application, and you want to get the access token, follow the following steps:

Step 1

Go to the following link:

https://developers.facebook.com/apps/

There if you do not have any app, create an app by clicking Add a New App.
After clicking that you will be asked to set the name of your App.
Give the name and click Create App ID(as shown below).

Step 2

Now you need to go to the following link:

https://developers.facebook.com/tools/accesstoken

There you will see your Application that you have created in the Step 1.
There you need to grant permission to get your User Token, or you can use your App token(as shown below).

Copy the App Token!

Step 3

As you have copied the App Token from the above step, paste it in the access_token field like:

Now if you hit enter, you will get the feed of the YouTube
page on Facebook in JSON form, like:

You can use multiple fields in the URL.
Like right now, there is only one field “feed”, you can add more to it by separating them with a comma.

On the following link:

(https://developers.facebook.com/docs/graph-api/reference/page/)

you can find various fields related to the nodes of Graph API of Facebook.

Now let us talk about the structure of Graph API.

Structure of Graph API

In Graph API, we use nodes to get data about individual objects.
We use edges to get collections of objects connected to a node, or to publish objects to those collections, and we use fields to specify which data you want included in responses.

Host Url

Almost all requests are passed to:

graph.facebook.com

host URL.
The single exception is video uploads, which use graph-video.facebook.com

Object IDs

Nodes are individual objects, each with a unique ID, so to get information about a node you directly query its ID.
For example, the official Facebook Page has the ID 20531316728.
You query it directly by using its ID:

GET graph.facebook.com/20531316728

Specify Fields to get Specific Data

If you want to get specific data, also known as field, about a node, you can include the fields parameter and specify which fields you want returned in the response. A quick check of the Page node reference reveals that one of the fields you can get when reading a Page object is the cover field, which is the Page's cover photo.
Here's what that query would look like:

GET graph.facebook.com/20531316728?fields=cover

Specify Edges of the Graph

Most nodes have edges, which can return collections of objects connected to that node.
To query an edge, you use both the node ID and the edge name.
One of the edges listed in the Page node reference is the photos edge, which returns all of the Photo objects owned by the Page.
So, to get all of the photos owned by the Facebook page, you query the node's photos edge:

GET graph.facebook.com/20531316728/photos

Update your Facebook’s Page Description

Some nodes allow you to update fields with POST operations.
For example, if you were an Admin of the Facebook Page you could update its description field like this:

POST graph.facebook.com/20531316728?description=The%20OFFICIAL%20Facebook%20Page

Publish Photos to your Page

Edges often allow you to publish new objects to the node's collections by performing POST operations.
Here's how you could publish a photo to the collection of photos owned by the Facebook Page:

POST graph.facebook.com/20531316728/photos

Of course, publishing an object to a collection typically requires additional fields about that object, such as a photo's URL, or a title, or description.
Edge reference documentation indicates which fields are required and which are optional.

Delete Operation in Graph API

You can usually delete a node by performing a DELETE operation on the object ID:

DELETE graph.facebook.com/20531316728

Summary

In this article, we have explored various facets of Facebook's Graph API.
We have also learnt various operations you can do by utilizing the Facebook API.
Auth process is kind of a cumbersome and confusing topic in API, which was covered in detail.
That's it for this article.
In future article, we will go under the hood of the Graph API of Facebook to get the greater insight.

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?