Facebook's Graph API

Facebook's graph API

This is a very short article and it explains Facebook's Graph Application Programming Interface (API) and how to read data from the Social Graph using HTTP's GET method. First of all we should understand what an API is and how it works.

An API works like a web site, we can make requests to an API and get a response back over the  HyperText Transfer Protocol (HTTP). There are various kinds of APIs (Twitter's API, Google's API and Facebook's API and so on).

REST API

Representational State Transfer (REST) works the same as a website does, we can make a call from a client to a server over HTTP protocol and get a response back.

Graph API

REST API is the original API for developing on the Facebook platform but a few years ago they decided to go in a new direction and launched the Graph API. Using this Graph API we can read data from and write data to Facebook's Social Graph. It's based on the HTTP protocol. We can do many things with the Graph API, we can request data, post new stories, upload photos and so on. Let's see how Facebook's Graph API works.

graph working

This is the drawing of Social Graph. Here dots/nodes represent people and the line between the dots/nodes represent a relationship between them.

graph flow

Getting data from Facebook Social Graph

As we all know Facebook's domain name is facebook.

Let us now take any Facebook page/User/Group to understand the Graph API. Here I will choose C# Corner's page CsharpCorner.

facebook page

Now let's replace www with graph in the preceding URL.

So it should be like CSharpCorner Graph and press Enter.

We can also see a person or group name instead of Page name.

page name

And here is what happened, we got the data from the Graph API in JavaScript Object Notation (JSON) format. This data is well structured and arranged in key and value pair format.

We can also request only specific fields if we want, you can see that in the following screenshot. I was trying to request some fields like ID, Name, Likes (Total Page Likes), and Location.

fields

You can check other fields (like gender, hometown, relationship_status, bio, quotes, political, birthday and so on).

Fetching data for someone (by UserID)

Type https://graph.facebook.com/{user_id}

fetching data

It gives only public information about me and my profile. We can also see someone's recent posts and their feed using.

https://graph.facebook.com/{user_id}/feed

feed

Some requests require an Access Token and in that case it will return an OAuthException.

OAuthException

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now