Get number of likes of a facebook page using Graph API in Python

Get number of likes of a facebook page using Graph API in Python

In this tutorial, Lets see how to get no. of likes and other page data of a facebook page using Graph API. No official python sdk for Graph API is available. We can use Graph API REST method to interact with API.

Since facebook graph api update, graph api requests need to be authenticated. Get Access token from https://developers.facebook.com/tools/explorer. This token is not a permanent access token, it will expire. For permanent access we need to create an facebook application and do oauth to get access token.

Code

import urllib2
import json

def get_page_data(page_id,access_token):
    api_endpoint = "https://graph.facebook.com/v2.4/"
    fb_graph_url = api_endpoint+page_id+"?fields=id,name,likes,link&access_token="+access_token
    try:
        api_request = urllib2.Request(fb_graph_url)
        api_response = urllib2.urlopen(api_request)
        
        try:
            return json.loads(api_response.read())
        except (ValueError, KeyError, TypeError):
            return "JSON error"

    except IOError, e:
        if hasattr(e, 'code'):
            return e.code
        elif hasattr(e, 'reason'):
            return e.reason

page_id = "idiotinside" # username or id
token = "TOKEN"  # Access Token
page_data = get_page_data(page_id,token)

print "Page Name:"+ page_data['name']
print "Likes:"+ str(page_data['likes'])
print "Link:"+ page_data['link']


Python modules used:

  • urllib2
  • json

Download

Download From Github