Tuesday, June 26, 2018

Calling Facebook Graph API

It is the continuation of previous blog. If you didnt look into the previous blog, it is better to look again. In this blog, we are going to see some small examples of calling facebook graph API.

GET operation
Get operations almost always begin with a node. A node is an individual object with a unique ID. For example, there are many Page node objects, each with a unique ID, and the Coca-Cola Page is the only one with the ID 820882001277849. To read any node, you query a specific object's ID. So, to read the Coca-Cola Page node you would query its ID.

Query using Graph API explorer 

To query via Graph API explorer in this scenario, you have to specify the object id after the version number. In this case, you need to have a valid aceess token.
As showed in the above image, you can get the name and id as the result.

Query using Postman

As the initial step of the query, you need to go to Authorization tab and select 'OAuth 2.0'. Then specify 'Authorization' as a key in the Headers tab. For the value of the key, you need to specify "Bearer" + " " + User Access token. 

With URL, you need to specify the object ID. The below image shows about this.
Query using CURL request

curl --header "Authorization: Bearer EAACEdEose0cBALx3pH3PxGRjZB6y644l2Th7ExQc0o7mN41qVrw4z3X1Y1QJPY0cNBN" https://graph.facebook.com/820882001277849

Here you need to specify, your access token after 'Bearer'

Getting all Posts from an object ID 

According to the object ID, we can get all the post publicly available in a object ID.





Query using Graph API explorer 
 Query using Postman
 Query using CURL request

curl --header "Authorization: Bearer EAACEdEose0cBALx3pH3PxGRjZB6y644l2Th7ExQc0o7mN41qVrw4z3X1Y1QJPY0cNBN" https://graph.facebook.com/820882001277849/feed


Getting a video/post comments in order

You can get the comments of a post using this.
And you can order results based on object creation time. To do this, use a .order() argument with one of the following values on either a field or edge.
  • chronological — orders results with the oldest created objects first.
  • reverse_chronological — orders results with the newest created objects first.
For example, let's get all of the Comments on one of the Coca-Cola Page's video Posts (1809938745705498), order the results chronologically (oldest first), and limit the number of objects per paginated result to three:

 Query using Postman
 Query using CURL request

curl --header "Authorization: Bearer EAACEdEose0cBALx3pH3PxGRjZB6y644l2Th7ExQc0o7" https://graph.facebook.com/1809938745705498?fields=comments.order\(chronological\).limit\(3\)

Publishing data to a node 
 Most edges allow you to publish objects to a collection on a node. You can do this by using a POST request on the node's edge. For example, you can publish a Comment on a Photo by using the Photo node's /comments edge:

 Query using CURL request

curl --header "Authorization: Bearer EAACEdEose0cBALx3pH3PxGRjZB6y644l2Th7ExQc0o7mND" https://graph.facebook.com/1809938745705498/comments?message=Awesome! 

In above we have see some examples to get and post data. You can delete also like above. 

Thanks :)

Monday, June 25, 2018

How to get Access token from the Facebook Graph API

Overview of Graph API
The Graph API is the primary way for apps to read and write to the Facebook social graph. From this API, we can get data into and out of the FB platform. User access token is need to deal with Graph API. How to get User access token will also discussed in this blog. The Graph API is named after the idea of a "social graph" — a representation of the information on Facebook. It's composed of:
  •  nodes — basically individual objects, such as a User, a Photo, a Page, or a Comment 
  • edges — connections between a collection of objects and a single object, such as Photos on a Page or Comments on a Photo
  • fields — data about an object, such as a User's birthday, or a Page's name

Host URL: graph.facebook.com

Object ID: 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. You can get your profile unique ID also from the Graph API explorer. The details of the Graph API explorer is given in below subsection.
If you want to get specific data (called fields) about a node, you can include the fields parameter and specify which fields you want returned in the response.

Versions: The Graph API has multiple versions. But here we'll explain how you make a call to a latest version 3.0. It's really simple: just add the letter v followed by the version number to the start of the request path. For example, here's a call to version 3.0.

 Overview of Graph API explorer

Graph API explorer is used to get information from Facebook and posting information to Facebook using the Graph API. Through this explorer, we cn easily get access token and run someget/ post/ delete operations.

You can find out Graph API explorer using this URL https://developers.facebook.com/tools/explorer

After you go to that URL, you need to specify the username and password of your Facebook Account. After you login, you can see a page as below image.

In that page, you can see a Get Token button. If you click on that button, you are able to get acess token. When you click on that button, you see a pop up like below image.

 Here you can select the fields, you want to aceess using the explorer. Here I have selected almost all. Then you need to click on 'Get Access Token'. Then you can see the Access token in the text box near to Access token. You can copy it for further use. This is not long term access token. So we can use it for specific short time. You can see the expire time by clicking on blue color info icon near to access token. It will be like below.

If it expired, then you can click on 'Get token' button to again get the Access token. 

Getting An Access token which never expires.... 

You can create an access token which never expires by concatenating Facebook App ID and App Secret with a pipe.
To get Facebook App ID and App Secret, you must create a Facebook for Developers account. Don’t worry it so easy than it sounds.

First go to https://developers.facebook.com/apps to create an App. Then follow the wizard. Click on the 'Add App'. Then you can see a pop up like below. You need to specify your display name to apply on App. You can see already filled contact Email address. If you want to change, you can change.


 After setting up your app then go to your  dashboard>settings>basic then click show App secret. When you are cick show APP secret, it will ask password again. You need to specify password.
Your access token will be your app ID|app secret .Don’t forget the pipe “|” between the app ID and app secret. For example, my never expiring access token will be 1179322318819014|a5axxxxxxxxxxxxxxx.

In the next blog, we will see about how to get/post/delete data using Graph API explorer, Postman and Curl request.