Update:
This blog post is outdated, a new and vastly upgraded version of Django Facebook is now available. Read more about it here.
For Fashiolista.com
we needed to integrate with the Facebook Open Graph API. The open graph API is a very exciting facebook project, which you can read about more here and here. The code at fashiolista.com allows you to register/login via facebook using the Open Graph API (similar to the old Facebook connect, but registration, instead of only logging in). Before you go try it out, fashiolista.com is aimed primarily at females so your girl friends facebook account is probably a better fit.
Im releasing the source code for Django Facebook on github. Its a very early release, but it might help other developers trying to implement a facebook register/loging flow using the new open graph api. See Github for requirements and installation instructions.
Update:
Birthday formats are currently giving some troubles for some users.
Fixed and tests added to prevent future problems. (note Fashiolista.com may still give errors, will be resolved during our next release).
Mellow Morning » Django Facebook – Open graph API implementation | Facebook F8 responded on 17 May 2010 at 2:39 pm #
[...] post: Mellow Morning » Django Facebook – Open graph API implementation // Tags: a-very-exciting, allows-you, facebook, graph, here-and, integrate-with, open graph, [...]
Jori responded on 17 May 2010 at 10:06 pm #
There are few more projects doing the same:
http://github.com/dickeytk/django_facebook_oauth
And django-socialregistration added Open Graph just recently:
http://github.com/flashingpumpkin/django-socialregistration
I have tried the first one and it worked fine.
tschellenbach responded on 18 May 2010 at 9:22 am #
Wish I had those around when writing this, interesting.
Looks like they don’t create actual users based on the facebook data though. Doing so makes implementation with the rest of your code far easier.
Andy responded on 18 May 2010 at 10:21 am #
Thanks for releasing the code!
Can I use Django Facebook to send status update back to the user’s facebook profile?
For example, if a user upload a picture to my site, I want her facebook friends to see in their news feeds something like “Amy uploaded a picture to foobar.com”
How do I do that?
Thanks
tschellenbach responded on 18 May 2010 at 11:07 am #
Thats pretty easy. First you need permission.
So instead of calling (see example.html)
var requiredPerms = ['email','user_about_me','user_birthday','user_website'];
FB.login(function(response) {
formElement.submit();
},
{perms: requiredPerms.join(‘,’)}
);
you add ‘publish_stream’ to the permission. For more permissions see:
http://developers.facebook.com/docs/authentication/permissions
With the next python view, you facebook user will have the ability to write.
facebook.put_object(“me”, “feed”, message=”Hello, world”)
Also see
http://developers.facebook.com/docs/api#publishing
Jori responded on 19 May 2010 at 10:43 am #
tschellenbach: Adding that shouldn’t be hard as Open Graph is really easy to query once you have the access_token (just poke around Facebook Python SDK and you get the idea).
If you want to post to user’s wall try the Python SDK. It might be better to use in many cases compared to JS library.
I don’t get why Fashiolista asks the access for birthday info. I think that this is really big privacy issue as there shouldn’t be a need for that info (it’s a shame that FB doesn’t allow opt-out as it does with email).
Thierry Schellenbach responded on 19 May 2010 at 11:34 am #
Indeed its a pity you can’t make permissions optional.
We don’t need to have your birthday, would like you to share it though.
Lån responded on 26 May 2010 at 1:16 am #
Hello. This looks like just what I need for my site. Will this enable visitors to register with their facebook profile and then my site (Wordpress) grab the name/age/profile picture/country & such? Im really a neeb and hope you can enlighten me!
Ubay Oramas responded on 27 May 2010 at 8:40 pm #
Thank you very much for all :D
Alu responded on 29 May 2010 at 6:44 pm #
Hi! I’m working in a website based on a php framework called Pluf… is your code adaptable to other frameworks? I’m looking through your code and I was wondering if you’ve added the “posting to profile” option, in the sense of posting messages ?
Thanks!
Thierry Schellenbach responded on 05 Jun 2010 at 12:28 pm #
Well the general structure would be similar in any language or framework.
As a starting point for an adaptation in another language i would mainly consult the facebook documentation though.
http://developers.facebook.com/docs/
Aaron Robinson responded on 08 Sep 2010 at 11:26 pm #
Hi Thierry,
First off, thank you very much for going to the effort of releasing this code; I have been trying very hard to find some up-to-date django/facebook code that works, and think I’ve finally settled on your code.
I am completely new to django and completely new to any Facebook API, and although there seems to be dozens of django+facebook projects, they all either don’t work with django 1.2.1 or are generally broken/undocumented… Most also seem to use older Facebook API’s, although I’d be happy using an older API if it meant I could get some code working :-/
I am wanting very limited functionality from Facebook – just the user’s details for the purposes of authenticating them, and later on the ability to post on their wall. Ideally I want this to run “inside” facebook like a regular facebook app rather than on a separate web site like Fashiolista does, but I assume that won’t be a problem (?).
I have been working through getting your code up and running, and due to some missing pieces of the puzzle (and a lot of missing knowledge on my part) it’s been *really* slow going.. I’m now running into a couple of bits that I can’t guess at and would greatly appreciate your help. I’ll go through what I’ve done so far:
- Downloaded Django-facebook from pypi – noticed there were a couple of files missing from the package meaning that installing it wouldn’t work – downloaded the package from github which only had a couple of missing text files stopping instillation I think (I’m also not that familiar with python package intricacies) – I added a couple of empty text files to the github package and it seemed to install okay.
- Started with an almost complete version of the django poll tutorial as a basis for implementing your code.
- Implementation step 1 (from your list on github): I have no idea what FACEBOOK_FAKE_PASSWORD and FACEBOOK_JINJA should be, so left these undefined, (I think they both default to True).
- Implementation step 2: done
- Implementation step 3: I assume this is all taken care of if I use your connect.html template as a basis? – I didn’t do anything else here.
- Implementation step 4: Eventually figured out that I had to call my UserProfiles class “Profiles”, and it had to be in a module called “user” (I changed my Model name and changed “from user ….” to “from gec ….” in auth_backends.py (as “gec” is the name of my app). I also found there needed to be “user = models.OneToOneField(User)” in my Profile class (although I’m assuming this name is standard anyway).
- Implementation step 5: Copied your connect.html file into my gec/templates folder (and hard-coded my own appId in it), added (r’^facebook/$’, ‘geconquest.gec.views.connect’) to urls.py, and set my Canvas URL in facebook to “http://localhost/facebook/” so it goes straight here.
To get over some CSRF errors I added “{% csrf_token %}” inside the in your connect.html…I think this is all I did there.
Now it’s looking for a form called RegistrationFormUniqueEmail in a module called “registration” (referenced from django_facebook/views.py:_register_user). I added a form here and put in:
> ip = forms.CharField()
> email = forms.EmailField()
just at a guess, and am now getting an import error “No module named coffin” from django_facebook/view_decorators.py:_process_response, which is something to do with this JINJA thing… I’m just about to try setting FACEBOOK_JINJA=False and see what happens.
Would you be able to brief me on the purpose of FACEBOOK_FAKE_PASSWORD and FACEBOOK_JINJA, and add in some skeleton files for any required files, with the required parts in (for example the contents of the RegistrationFormUniqueEmail class, as I have no idea what fields should be set here..)
Lastly, if you’re reading this thinking “this guy’s on completely the wrong track – he should be using the django-blah-blah-facebook project instead…” please let me know!
Thanks again,
and any comments/help from anyone else are most welcome!
Thierry Schellenbach responded on 04 Oct 2010 at 10:04 am #
Hey Aaron,
Sorry for the codebase being a bit raw at the moment. Everything you mentioned is perfectly doable with the combination of django/django-facebook and this version of the open graph protocol. Note though that some support for building facebook apps isn’t completely finished in this codebase. (The hashing for safety is somewhat different.)
When I published the code Facebook’s docs didn’t update yet to describe how it should work.
Anyhow, focusing on the user registration part. FACEBOOK_JINJA you should set to False. (That should have probably been a better default for most users, Jinja is a custom template engine, which only a subset of django sites use, since its faster)
FACEBOOK_FAKE_PASSWORD
If this is set to true, facebook users get a random password. (For compat with the default user model). Set it to false if you would rather redirect them to a form to fill in a custom password.
This code is not very friendly yet. Though i agree with your assesment that all the other libraries are broken or use the old api’s.
Hope you got it working :)
PS.
The Profile in user/models.py setup is a default django approach. Probably should have had that as a setting though.
Mitch responded on 19 Oct 2010 at 3:21 pm #
I’m seeing a problem on your site and am wondering if it is a problem with your “django-facebook” code or your site?
To recreate:
1) log into your facebook account
2) register on fashiolista using facebook connect
3) log out of facebook and fashiolista
4) try logging into fashiolista via facebook connect
Result: “An error occurred with Fashiolista. Please try again later.”
I think it will be a pretty common case where the user is logged out of both facebook and fashiolista and needs to log back in. Since they don’t have a username/pw that they know, they’ll need to log back in via facebook connect. If they can’t, that’s a pretty big problem.
Are you aware of this problem and is there a fix in the works? I’d hate to spend the time integrating django-facebook and have the same thing happen on my site.
Thanks for the demo page and for releasing the code. Hopefully you can fix this problem and become the de-facto django/facebook connect app!
Jason responded on 22 Jun 2011 at 8:37 pm #
You can fix the above issue by adding a FB.getLoginStatus call or setting status to true in FB.init in javascript. This will ensure you have a valid session with facebook.
Graphs responded on 13 Dec 2011 at 9:42 am #
Information related to open graph is wonderfully judged. It is awesome indeed.