Created
September 26, 2011 05:02
-
-
Save KyeRussell/1241647 to your computer and use it in GitHub Desktop.
Facebook API Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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