Facebook API and Python

super9 picture super9 · Mar 4, 2011 · Viewed 30.9k times · Source

Does anyone know of a good platform agnostic example or library that does Facebook authentication and Graph API access via Python?

The official Facebook Python SDK is tied to Google App Engine and Pyfacebook is deeply woven with Django.

I just want to be able to mess around in terminal and go through the process of authenticating a user and then doing simple requests from the Facebook API.

Thanks.

Answer

sharjeel picture sharjeel · Mar 4, 2011

I ran across same problem some time ago and later found out that PyFacebook isn't deeply tied up with Django. It just uses a few utils from django.

My recommendation is that you setup PyFacebook alongwith django and then play around with it using command line. To use PyFacebook you won't have to go through or even know anything about django at all.

Here's an example:

from facebook import Facebook

api_key = 'Your App API Key'
secret  = 'Your App Secret Key'

session_key = 'your infinite Session key of user'

fb = Facebook(api_key, secret)
fb.session_key = session_key

# now use the fb object for playing around

You might need to get an infinite session key which you can get from here: http://www.facebook.com/code_gen.php?v=1.0&api_key=YOUR_API_KEY

Use this code to get convert the code from above URL into infinite session key:

def generate_session_from_onetime_code(fb, code):
    fb.auth_token = code
    return fb.auth.getSession()
print generate_session_from_onetime_code(fb, session_onetime_code)