Getting Python to work with Facebook's OAuth 2.0 authentication system

I recently treaded into the dreaded territory of the Facebook API to try to create a viral app on top of AvantCard. I ran into a lot of snags that I'll share, and luckily found the solutions (which I will share as well).

The big thing I learned the hard way was that Facebook is requiring OAuth 2.0 authentication by September 1, 2011 and their Python-SDK does not support that.

I found this out by first not trying to use OAuth 2.0 and being OK with the Python-SDK, but discovered that the non-OAuth Facebook cookie (with the key "fbs_[your_app_id]") was not storing in Chrome for some reason. After I figured that out, I switched to OAuth 2.0 by changing a setting within FB.init() in their Javascript SDK and was able to get the correct cookie saved (with the key "fbsr_[your_app_id") in chrome! But... even with the cookie, the Python-SDK wasn't getting it. 

So I went into facebook.py and tweaked the cookie-getter function to look for "fbsr_" vs "fbs" and then discovered they were just completely different and even doing that wouldn't fix the problem.

And along came this handy-dandy website that clarifies all this. They pointed out the problem I discovered after trying to deal with it myself, and created a workaround. Generate the login-url, and authentication-url's with Python. Don't let Facebook's Javascript SDK suck you in.

So I did just that, and it allowed me to successfully, get a user to the log-in screen, and generate the cookie, and get the cookie to get access to the Facebook access_token for that user. The only thing I need to do is throw in some JQuery to let the user not have to physically switch web-pages to do a Facebook login, and simply have a pop-up.

 

The other handy-dandy thing I fell upon was a pre-made function to handle FQL queries in Python really easily by just adding some code into the facebook.py file in the Python-SDK. Here's the site. Even though the site has you put the function inside facebook.py, I just modified it slightly to work in my controllers file for my Django/Python app.

 

Hopefully if anyone else out there is trying to get their Python app to work with Facebook's new OAuth 2.0 authentication system, you found this helpful.