django + facebook connect

Apr 02 2009

While working on News Mixer and another django project, I’ve refactored all the facebook connect code Brian Boyer and I wrote into a separate application. I’ve opened a google code page for it. I figured if it’s useful to me, it would be useful to others.

It’s a simple thing really. It uses the built in django auth stuff and pyfacebook, and works with django-registration. News Mixer was designed so you had to use facebook and only facebook to log in. The django-facebookconnect application can work side-by-side with regular django user accounts, and it gives new facebook users the option to link their django and facebook accounts.

While building News Mixer, we had a lot of problems getting facebook connect to play nice. Timeouts, expired sessions and connection resets would quickly kill our application. On the face of it, the app doesn’t really do a whole lot, but under the hood it deals with a ton of BS problems that you run into using the facebook API extensively.

Take it, use it, let me know how it goes.

10 responses so far

  1. Awesome. Definitely going to be useful having different login options. This could be very handy very soon.

    So, if people have already created accounts through django.contrib.auth, and they log in via FB, would it create a new account? or auto-magically find their existing account?

  2. When someone logs in with their facebook for the first time, they get two choices: to link with an existing account or just use facebook. Folks who opt to just use facebook will always have to login to the site with that facebook account. Folks who choose to link their account can login with either facebook or the old django auth account.

    I’m thinking it would be cool to add a third option to create a new account to associate with facebook. So somebody could log in with a django username and password if they want.

  3. That makes sense. I like the third option, especially since it gives me (as a site builder) the option of dumping FB connect if it starts to get onerous or just Orwellian without losing a bunch of accounts.

    People have been asking about using FB connect on Tools for News. This might be the head start I need to actually make it happen.

  4. [...] django + facebook connect – Ryan Marks has a Django app for a better Facebook Connect. [...]

  5. Hi, I’ve installed your facebook-connect app but I’m having problems understanding how to wire it up with the views. And also how to load templates, etc. Could you please give a little more detail?


  6. I have a problem. I followed the instructions above and applyed it to a fresh installation, but when I synced it to the database och got the message:

    Error: No module named adminfacebookconnect.

    Do someone now whats the problem is?

  7. What do you think it would take to modify this code to run on Google App Engine (with the django patch). Is it only a matter of rewriting the models that you have to use GAE model instead of the django models?


  8. I’m not sure – if you rewrite the model, it might work. As long as pyfacebook works on GAE. I’ve also started playing with using thread local variables to store some data to reduce the number of facebook calls. The facebook model does a lot, but you can probably just copy that stuff all into a new object that uses GAE backend.

    Hope this helps

  9. I saw you started on a oauth branch, is it production material or does it still need some work? If so is there anything I could do to help? Any tips on how I should migrate to oauth?


  10. @Miika – Don’t use it. It’s old and uses stuff that Facebook has deprecated. The OAUTH branch is something I started messing around with in order to get my old apps working with oauth and the open graph stuff. It doesn’t work yet.