Facebook API | Set-1
In this series of articles, we will cover the details about Facebook API in depth. We will learn what setup we need to use the API and how to make everything get going for the API to work. So, Let’s skip the Introduction and Jump on to the details of the API.
There are 2 sets of API Available from the Facebook Platform –
- Graph API
- Marketing API
Let’s first start with the Graph API.
Graph API is the primary way to access means read and write data on the social media Facebook social graph. We will first discuss the overview of the Graph API, proceeding to the setup, we will then finally see how to use Graph API Explorer for reading and writing data on Facebook Social Graph.
Graph API is basically composed of 3 components:
- node: These are individual objects such as User, Page, Photo or a Comment. We use Nodes to gather data about individual Objects.
- edges: These represents the connections between collections of objects and single object. Edges are used to publish single objects to the collection of objects or to get collections of objects that are connected to a single object. Suppose we consider a Photo posted on Facebook Profile of an Individual. Now the comments associated with this Photo refers to the collection of Objects that are associated with the single Object that is Photo in our case.
- fields: These represents the data or Information about an object. User Age, Date of Birth are examples of fields that are associated with a node User. fields are used to specify the data that we want to include in a response.
Since Graph API is HTTP based, so it works with any Language that has HTTP Based Library like curl, urllib etc. All request are passed to the
graph.facebook.com host URL. Exception to this is Video upload which uses the URL:
Since Nodes are Individual Objects on Facebook. So each node has an associated unique Id. Every User, Page or Photo has a unique Id associated with it. Before proceeding further let’s first discuss Access Tokens and how to generate one to use for querying data from Facebook Social Graph.
Access Token here majorly serves 2 purposes:
- It allows the app to access a User’s Data without requiring User’s credentials.
- It allows the server to identify who is using the app and to determine accordingly what functions that user is permitted to do.
Access Tokens conform to
OAuth 2.0 Protocol. This means Access Tokens implement both Authentication and Authorization, as we have discussed in the last article.
Here, we have 4 types of Access Tokens:
- User Access Token: This is the most commonly used access token, needed when an app calls an API to read, modify or write data on specific person’s Facebook Page. This is achieved through a login dialog where a person is required to permit the app to obtain access token
- App Access Token: This token is obtained using server to server call. This token is used to read, modify the app settings.
- Page Access Token: Once you have the User Access Token you obtain the Page Access Token via the Graph API. The procedure is very simple, first, obtain the User Access Token and then ask for the manage_posts permission. You will get your Page Access Token. This token is very much similar to the User Access Token, except for the case where it provides permission to the API that read, modify or write data to a Facebook Page.
- Client Token: Client token is an identifier that can be embedded in native mobile binaries or desktop app. It is used to access app-level APIs.
Follow the steps below to generate Access Token:
- Go to the link Facebook Developers and in the right most corner you will find an option to “Login”. If you are not an existing customer create a new account by clicking on the “Login” option and then clicking on the “Create New Account” button appearing below the Login button.
- After Successful login or a successful account creation you will get previous screen with “My Apps” option at the place of Login.
- Go to “My Apps” drop down in the right most corner and select “Add New App” . Enter Display Name and Contact Email and click on “Create App ID”.
- After entering the required information, you will see the Home Screen of your Facebook Developer Account, with App ID mentioned on the top.
- Click on the “Tools” Drop Down and from the list click on first option i.e.”Graph API Explorer” Option.
- On the Graph API Explorer window click on the “Get Token” button. From the drop down list, click on “Get User Access Token”.
- On clicking on the “Get User Access Token”, you will get a huge list of User permissions grouped under 3 categories: User Data Permissions, Events, Groups and Pages and Other Permissions. You can skip the permissions that you don’t want. After making the selection, click on “Get Access Token”. You will get a screen for Login review.
- After successful completion of above steps you will get your User Access Token.
- Now once again click on the “Tools” Drop Down on the same page and now this time click on the second option “Access Token Debugger”.
Note: Before clicking on Access Token Debugger Option, Copy the User Access Token generated in the previous step.
- Now Paste the user access token in the dialog box stating Enter an Access Token to debug and click on the “Debug” Button.
- After clicking on Debug Option You will see some information about your user access token. Below those details, there is an option to “Extend Access Token”. Click on this button. You will be asked to enter your Facebook Login Password and You are done!!!. The token that was about to expire in 2 hours will have life increased to 2 months. This extra step ensures that your access token does not expire in every 2 hours.
In the next article, we will be discussing how the User Access Token can be used for accessing different types of information from the Facebook Social Graph.