Skip to content

Instantly share code, notes, and snippets.

@KyeRussell
Created September 26, 2011 05:02
Show Gist options
  • Save KyeRussell/1241647 to your computer and use it in GitHub Desktop.
Save KyeRussell/1241647 to your computer and use it in GitHub Desktop.
Facebook API Example
import urllib2
import json
class FBParser(object):
"""Receive Facebook Open Graph API calls and return an object containing
the results"""
def call(self, call, api="https://graph.facebook.com/"):
"""Process Facebook API calls."""
# Grab the content.
remoteURL = urllib2.urlopen(api + call)
remoteText = remoteURL.read()
# Parse the json with json.loads() and return the resulting object.
jsonObject = json.loads(remoteText)
return jsonObject
# Create a new parser object.
parser = FBParser()
try:
# Make a Facebook API call to /KyeRussell and print out the first_name and last_name.
myprofile = parser.call("KyeRussell")
print("This profile belongs to: " + myprofile['first_name'] + " " + myprofile["last_name"])
except KeyError:
# There was an error parsing the object, chances are that the returned object isn't a personal profile.
print("The object is not a personal profile.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment