How to use the Facebook Graph API Explorer

Getting data out of the Facebook Graph API initially can be trivial. Especially if you are trying to get data for a Facebook Page and not a personal Profile. This is a tutorial on how to get started with the API explorer and to request common data such as fan count, published posts and post share counts.

Additionally, this small tutorial should hopefully give you the confidence to try some advanced API calls using some JavaScript or PHP code.

Requirements

  • A Facebook Account (For getting Access Token)

Loading up the Graph API Explorer

Firstly Navigate to https://developers.facebook.com/tools/explorer/

One of two things will happen, the API Explorer will request you to log in first, or if your already logged into Facebook, you will see the following page pre-loaded with an access token and the JSON for the parameters account id and name.

Notice the /me? in the Get request URL, if you hadn’t guessed already, it’s an alias for your account’s username. In this case the First Name & Last Name of the account logged in is Dan McStephens

So lets get started, we shall read some data that relates to the page(s) that is managed by my Facebook Account.

Reading Managed Facebook Pages Data

Step 1 – retrieving the basic data
  1. Click the Get Token button.
  2. Tick the box for the pages_show_list permission under the Events, Groups & Pages section.
  3. Click the Get Access Token button.
  4. Facebook will load a new window with the message Graph API Explorer would like to show a list of the Pages you manage.
  5. Click the Ok button.
  6. Using the Node pane on the left hand side, search for the accounts field and select it. The current GET URL should now match me?fields=id,name,accounts

Depending on how many pages you manage, you should get a similar response to the following.

{
  "id": "112355669601458",
  "name": "Dan McStephens",
  "accounts": {
    "data": [
      {
        "access_token": "EAACEdEose0cBAP5PzlUmW8xQZCvSPIgPc0NVi5mwltuZCHH6ZBVOjUMvhynZAXHp13heMZCVaAZCrdPGWP9gSYhrZBo3m0FszfTMJRQ8DbhEpr9ZB5TCKgEAiM5dsW6ZAzLqr8kitl0x7JnM3hKWZCMEjhNawht3zsADNbPDNkZBQwWKMDBBrOATjPqAA7ZAeuFSLYo3oYLjhQnEivAYhUEmomfh",
        "category": "Blogger",
        "name": "Code Addictz",
        "id": "578536312523805",
        "perms": [
          "ADMINISTER",
          "EDIT_PROFILE",
          "CREATE_CONTENT",
          "MODERATE_CONTENT",
          "CREATE_ADS",
          "BASIC_ADMIN"
        ]
      }
    ],
    "paging": {
      "cursors": {
        "before": "NTc4NTM2MzEyNTIzODA1",
        "after": "NTc4NTM2MzEyNTIzODA1"
      }
    }
  }
}
Step 2 – Requesting further information about the page(s) you manage

Now we can start selecting some child fields from the accounts parent field.

  1. Search for the following fields and select them.
    1. fan_count (Likes).
    2. posts
  2. Using the posts field, add the following child fields.
    • shares
    • full_picture
    • created_time
    • caption
    • name
  3. Your get url should now look similar to the following – me?fields=id,name,accounts{fan_count,posts{shares,full_picture,created_time,caption,name}}
  4. Go ahead and hit the Submit button

The request should display something like the following screen shot.

As you can see, we have a fan_count of 2, an array of posts displaying the full URL to their pictures, the timestamp they were created at, post caption, identifier and the name of the post.

So what if we don’t want to see a page that we already manage? For example, we want to see data from CocaColas company page. Well, we do it like so…

Reading Specified Username Facebook Pages Data

Step 1

Modify the get request URL to match the following cocacola?fields=name,id

Step 2

Add fan_count & posts field

Step 3

Hit the Submit button

Step 4

Lets get both the Share count & the Like count of the posts.

Modify the GET URL from

cocacola?fields=name,id,fan_count,posts to cocacola?fields=name,id,fan_count,posts{shares,likes.summary(true)}

Hit the Submit button again.

The API should now of returned JSON like the following –

{
  "name": "Coca-Cola",
  "id": "40796308305",
  "fan_count": 107377711,
  "posts": {
    "data": [
      {
        "shares": {
          "count": 2
        },
        "likes": {
          "data": [
          ],
          "paging": {
            "cursors": {
              "before": "MzQ2MDU5MDc1NTkyNDA1",
              "after": "MjAyMjA1MjI3MjE3NzI4"
            },
            "next": "https://graph.facebook.com/v2.12/40796308305_10157126484688306/likes?access_token=EAACEdEose0cBAD9vzEvsLQVFbqV43ZA2ZAzg1ZAwMdRYA72T7PQ3l33FZAoH48Iu692JaAfwp8dZAG0408UAgrTBqw6iLs6vIiZCCXnQ2vZAX83XFL8jRbSGZB5SeUfe7ZAIyd6o3OGsR2mZBTrHto80xlK6ZAtEtaOXR15HAZCggDZAoWyAR9qHC2d4ZCbdeoBY86ly5qR5MbB7pva63GyNNuXjXH&pretty=0&summary=true&limit=25&after=MjAyMjA1MjI3MjE3NzI4"
          },
          "summary": {
            "total_count": 87,
            "can_like": true,
            "has_liked": false
          }
        },
        "id": "40796308305_10157126484688306"
      },
      {
        "shares": {
          "count": 8
        },
        "likes": {
          "data": [
          ],
          "paging": {
            "cursors": {
              "before": "MzQ2MDU5MDc1NTkyNDA1",
              "after": "MjI0NDkyMDExMzQzODM0"
            },
            "next": "https://graph.facebook.com/v2.12/40796308305_10157039366093306/likes?access_token=EAACEdEose0cBAD9vzEvsLQVFbqV43ZA2ZAzg1ZAwMdRYA72T7PQ3l33FZAoH48Iu692JaAfwp8dZAG0408UAgrTBqw6iLs6vIiZCCXnQ2vZAX83XFL8jRbSGZB5SeUfe7ZAIyd6o3OGsR2mZBTrHto80xlK6ZAtEtaOXR15HAZCggDZAoWyAR9qHC2d4ZCbdeoBY86ly5qR5MbB7pva63GyNNuXjXH&pretty=0&summary=true&limit=25&after=MjI0NDkyMDExMzQzODM0"
          },
          "summary": {
            "total_count": 179,
            "can_like": true,
            "has_liked": false
          }
        },
        "id": "40796308305_10157039366093306"
      }

References

Here is a link direct to the Facebook Docs for the Graph Explorer fields or root nodes. The page will help you quickly look up fields that you want and what permissions etc that are needed to use and view them.

Graph API Reference

Summary

In this tutorial we learned how to initially start using the Facebook Graph web-interface. By using our own Facebook id to retrieve information about the Pages we manage. Additionally, we learned how to specify any Facebook Page username, namely CocaCola in this example. We were able to read statistical data like like counts, share counts and even get the full URL paths to images.

I hope this tutorial helps you on whatever projects you may be working on.

Interesting Links

Thanks for reading and as always, comments are welcomed.

How to use the Facebook Graph API Explorer

You May Also Like

Leave a Reply

Your email address will not be published.