Friday, December 11, 2015

Step by Step Access Your Facebook Page Information and Statistics

Although Facebook publishes information on how to use their API, there are not enough examples and the information on how to set up everything is not very clear. I have found many questions/answers in different web sites asking on how to do this and how to do that.

It is highly recommended that you read Facebook's Graph API.


The intention of this post is to help you get started and the first thing to do is to have a Facebook page. Once you have your page and some likes on it follow the next steps:


  1. Go to https://developers.facebook.com/ and login
  2. Register as a Developer




  3. Once you are registered you will be able to add apps to Facebook, this is needed in order to extract information.
  4. Click on "Add a New App" if is not open, otherwise just choose "WWW" Website.



  5. Click on "Skip and Create App ID"



  6. On the next screen, select an App name, in the category select Application for Pages and then click on Create Application Identifier.



  7. You application Dashboard will appear, from there you can get your application ID as well as your Application Secret ID.



  8. Now you have your application ID that you can use to login to Facebook.


The Graph API is a low level HTTP based API that allows you to query data, post new stories, upload photos and other tasks. The API will work on any programming language or tool that supports HTTP such as cURL and urllib. In our examples, we are not going to use any specific tool but row HTTP and since the interface uses SSL we are going to use  openssl in order to open a raw socket.

Every element in Facebook is called a Node and each node has an ID, you need those IDs in order to extract information from the. Please refer to the Graph API for more details on this.

You can get your node id by looking at the address bar or when you put your mouse over an object in the status bar.




For using openssl I am using a Cygwin terminal. 

The first thing we need to do is to get the access key, that key will allow us to use the rest of the APIs.

For getting the access key I will send the following command:

GET /oauth/access_token?client_id=<app id>&client_secret=<app secret>&grant_type=client_credentials HTTP/1.1
Host: graph.facebook.com

Where the <app id> and the <app secret> were taken from the Application Dashboard. 

Note that you need to press Enter twice after you enter the last line:

GET /oauth/access_token?client_id=<app id>&client_secret=<app secret>&grant_type=client_credentials HTTP/1.1
Host: graph.facebook.com
<crlf><crlf>

Lets do an example on how to obtain the access_token:

  1. Connect to Facebook API:  openssl s_client -connect graph.facebook.com:443
    After issuing this command, many printouts  appear in the screen
  2. Then we type or better just copy an paste the following, remember to update your application id and secret id, the onces below are fake (press Enter twice):
    GET /oauth/access_token?client_id=5153436434343&client_secret=43434h4347851fb&grant_type=client_credentials HTTP/1.1
    Host: graph.facebook.com



Once you are connected you can use the API in the same manner, lets do another example but now lets retrieve the page information. 

Please follow the next steps:


  1. Connect to Facebook's API (following the previous steps), if you are already connected, just copy the obtained access_token .
  2. Update the following request with the obtained access token and with your node (page) id:

    GET /v2.5/170838892958036?access_token=515365081974110|5gM5WkAfUZLUm7f2n8t0_icPqb0 HTTP/1.1
    Host: graph.facebook.com
  3. Copy and paste it to the terminal, remember to put two Enters at the end.



    When you paste the request it will be next to the access_token, see the underlined text, do not worry, a space or an enter is not necessary because the previous HTTP response includes a specific number of chars, so you can paste the request there. The text with the green line to the left is the HTTP response headers and the text with the blue line is the API response.

    {"name":"Visita Mexico","id":"170838892958036"}

You will have access to public available information and this is because of the permissions you have. If you try to request information for a metric or parameter that your do not have permissions you may get empty data, for example, if we want to retrieve the total number of people who have liked the page we need to use the following request:

GET /v2.5/170838892958036/insights/page_fans?access_token=fUZLUm7f2n8t0_icPqb0 HTTP/1.1
Host: graph.facebook.com 

Where the number 170838892958036 is the node(page) id and the access_token is the one previously obtained, since I do not have permissions I will get the following:


{"data":[],
"paging":
{"previous":"https:\/\/graph.facebook.com\/v2.5\/170838892958036\/insights\/page_fans?access_token=515365081974110\u00257C5gM5WkAfUZLUm7f2n8t0_icPqb0&since=1449087470&until=1449346670",
"next":"https:\/\/graph.facebook.com\/v2.5\/170838892958036\/insights\/page_fans?access_token=515365081974110\u00257C5gM5WkAfUZLUm7f2n8t0_icPqb0&since=1449605870&until=1449865070"
}
}

Please ignore the access_token since it is not the real one but note that the response contains "data": []  which is empty, the expected one should look like:


{
  "data": [
    {
      "name": "page_fans",
      "period": "lifetime",
      "values": [
        {
          "value": 50,
          "end_time": "2015-12-05T08:00:00+0000"
        },
        {
          "value": 50,
          "end_time": "2015-12-06T08:00:00+0000"
        },
        {
          "value": 50,
          "end_time": "2015-12-07T08:00:00+0000"
        }
      ],
      "title": "Lifetime Total Likes",
      "description": "Lifetime: The total number of people who have liked your Page. (Unique Users)",
      "id": "170838892958036/insights/page_fans/lifetime"
    }
  ],
  "paging": {
    "previous": "https://graph.facebook.com/v2.5/170838892958036/insights/page_fans?access_token=CAACEdEose0cBADYixYmUPSmjTZBAjNTDyZCBm5omZAQV6KFwEZBWBM6XUVSr7p3ClYiFHcaocjs3d323nAExn2Sn3pOPKudsZC43wRFYOVON5VitrPUEvHbKIFofb3o1TAJteaCqSxZCo9Pf0Fda6VGqizeKE00ZAJ2P8swfhyS9FKbGBvRZBXqpZAYezP2ZALal8DQ4Uz95iITI9d2OWzbJzs&debug=all&format=json&method=get&pretty=0&suppress_http_code=1&since=1448913822&until=1449173022",
    "next": "https://graph.facebook.com/v2.5/170838892958036/insights/page_fans?access_token=CAACEdEose0cBADYixYmUPSmjTZBAjNTDyZCBm5omZAQV6KFwEZBWBM6XUVSr7p3ClYiFHcaocjs3d323nAExn2Sn3pOPKudsZC43wRFYOVON5VitrPUEvHbKIFofb3o1TAJteaCqSxZCo9Pf0Fda6VGqizeKE00ZAJ2P8swfhyS9FKbGBvRZBXqpZAYezP2ZALal8DQ4Uz95iITI9d2OWzbJzs&debug=all&format=json&method=get&pretty=0&suppress_http_code=1&since=1449432222&until=1449691422"
  }

}

In the above response you can see that the page has 50 likes. 

There are many statistics and information that you can retrieve from Facebook, check all API options. If you are interested in metrics about your Facebook Page such as the number of stories generated about your Page, the total number of people who liked your Page, etc. look at Facebook's Insights Metrics (more info in Insights Metric). 
  

In order to get the extended permissions you have to first submit your app for review, in general the steps:


  1. Go to your application dashboard.
  2. Click on "Status & Review"
  3. Click on "Start a Submission"




  4. You will have to fill the form and follow all the steps. The approval process takes around 7 business days.



No comments:

Post a Comment