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 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 file in the Python-SDK. Here's the site. Even though the site has you put the function inside, 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.

7 responses
I think you might find that interesting:

(works for me)

Thanks! I will try that on my next Django Facebook App. Luckily I got a basic fix going for this one.
Thanks for the post. Exactly the problem we had.
No problem! I was hoping I could help someone like that. Since it was such a problem for me :(. Im surprised Facebook hasn't addressed this yet...

Thank you for the post. I was wondering, does this implementation allow you to also use social plugins supported by facebook as : like, share, comment ...etc

@Saher of course! But to optimize for those, I'd use a mix of the python API with the facebook JS JDK here:
Nice, thanks for the article. I also wanted to know how to build on using the facebook authentication for REST based services. I have a couple of ideas but just wanted to see what people are doing.