Facebook and Python – The Basic Sauce

Welcome to the Basic Sauce – API School is now a go!

So you want to bash stuff together to make that fancy 1M app in Facebook? You wonder, Why am I so stuck? Where is the typo? What’s wrong with the access token? What are my permissions? Why am I slamming my head on this computer. Where is Facebook Support? What’s happening with this Error Code 69: Error occured Type: ZuckException. So let’s clear this up and in the language of Python.

Sit back, relax and take in what I give for you to conquer this sheet

  • The Graph API is an interface utilizing various calls through http://graph.facebook.com (and http://graph-video.facebook.com, no need to fill your head with that now, get that on some extra special sauce)
  • The calls that we will use are either publicly accessible or need some form of authorization via an access token. The access token is your key to city, but each city has different keys. So get it right and be sure you know where you at. Today we are using a user access token. This can be used to make requests to the Facebook API on behalf of the user. Most of the time, this is all you need

Get yourself a proper scoped access_token at https://developers.facebook.com/tools/explorer/

* Ensure you pull from the drop down your application, notice it is currently set to Graph API Explorer

For /me/posts, we are going to need an access token (duh), but the reason is that there needs to be a way to figure out which user we are calling “me” for. There doesn’t need to be a need for extra permissions, let’s leave that for the extra sauce.

or you can retrieve it here if you already set up your permissions for the access_token at https://developers.facebook.com/tools/access_token/

Now that you are all pimped out, have that saved in .txt file somewhere nearby on your screen.

Let’s choose a library and post a lil some’n some’n without slapping yourself in the face

https://github.com/jgorset/facepy

Using the package manager, (you are using the package manager, right?)

You are using the package manager right??

Fire up terminal and we shall install

pip install facepy

From here, forget Vim, forget eMacs, forget Python files, we gonna to do this raw! RAWR Grab the access token from your .txt file then return to terminal and launch python

python

Now we are in interactive mode, import the facepy module and get the reference to GraphAPI object

from facepy import GraphAPI

This gives us the ability to run amok, wooo yeah!. Get settled, and grab that access token and insert as follows

graph = GraphAPI('your_access_token_goes_here')

Then check if we are a valid by pulling your personal information

graph.get('me/posts')

Congrats homie, you have used the API, wasn’t that hard was it?

Let’s up the level by posting a message, tune in for more sauce…

Comments

  1. [...] you fancy huh, now able to retrieve your personal posts and ish. Some of you have even figured out [...]

  2. [...] Well, back to cloud zero, time to make this work in Python. I shouldn’t have to tell you how to start up Python and load up facepy library again do I? [...]

  3. [...] I hope you are opening FacePy by now… take off those Graph API Explorer training wheels, we ar… [...]