My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Installation  
Updated Sep 11, 2009 by dryanm...@gmail.com

Make sure you have pyfacebook installed. You can get code and instructions on github: http://github.com/sciyoshi/pyfacebook/tree/master

django-facebookconnect only relies the django auth app, but it plays nice with django-registration if you would like to give your users the option to log in without facebook.

Add facebookconnect and django auth to your INSTALLED_APPS:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.sessions',

    'facebookconnect',
)

Both pyfacebook and django-facebookconnect have middleware that needs to be used. Middleware is a funny thing and is sensitive to the order in which it is executed. This order works:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'facebook.djangofb.FacebookMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'facebookconnect.middleware.FacebookConnectMiddleware',
)

If you have other middleware, you may have to experiment with where in the list you should put your middleware. If you change the order of the stuff above, it will break.

Add the facebook to your authentication backends. If you dont have an AUTHENTICATION_BACKENDS directive in your settings, just use this one:

AUTHENTICATION_BACKENDS = (
    'facebookconnect.models.FacebookBackend',
    'django.contrib.auth.backends.ModelBackend',
)

One more thing you may want to know about is the dummy facebook data. If, for whatever reason, the code can't retrieve facebook profile information for a user, it will use dummy data. Facebook Connect drops requests from time to time, so instead of killing the application, it will just show something. You can set the DUMMY_FACEBOOK_INFO directive in the settings file to change the default stuff. Here is what the default stuff looks like:

DUMMY_FACEBOOK_INFO = {
    'uid':0,
    'name':'(Private)',
    'first_name':'(Private)',
    'pic_square_with_logo':'http://www.facebook.com/pics/t_silhouette.gif',
    'affiliations':None,
    'status':None,
    'proxied_email':None,
}

facebook API

Setup a facebook applicaiton here: http://upload.facebook.com . You'll need to have a personal facebook account to set this up. Once you add a facebook app, go to the connect section of the setup and provide the url for your facebook connect site (developers can enter localhost). Once you save the settings, you get an API key and secret. In your django settings file enter this stuff:

FACEBOOK_API_KEY = '00000000000000000000000000000000'
FACEBOOK_SECRET_KEY = '00000000000000000000000000000000'
FACEBOOK_INTERNAL = True

The third setting is for pyfacebook. I don't know what it does completely. But it does cause pyfacebook to hold on to expired facebook sessions when it's False.

caching

So you have to use caching with facebook connect. Especially if you plan to display profile info from your facebook connect users. Caching is used no matter what, but if you don't configure it, django will just cache to memory for the execution and not for the long term. Or something. It's probably bad.

So read this http://docs.djangoproject.com/en/dev/topics/cache/ and setup database caching or memcached. There is one more setting directive you might be interested in:

#Cache facebook info for x seconds. Default is 30 minutes
FACEBOOK_CACHE_TIMEOUT = 1800

Django's cache framework rocks by the way.

Using facebook connect

Once you've got everything installed, all user objects will have a 'facebook_profile' object hanging off them. That is if the user has logged in with facebook. The FacebookProfile object has all sorts of nifty methods and properties.

There are templates that you can override. The setup screen is presented to a new facebook user when they first log in. A user can then choose to link their facebook account to an existing account or not. To get these views add the following to your project's url.py:

urlpatterns = patterns('',
    ...,
    (r'^facebook/', include('facebookconnect.urls')),
)

There is a template tag library called facebook_tags which has a bunch of shortcuts for displaying facebook profile information for a user. Put

{% load facebook_tags %}

at the very top, immediately after any {% extends %} statement. Put

{% facebook_js %}

in the html head section and put

{% initialize_facebook_connect %}

at the bottom of your base.html file before the closing body tag. It will install the facebook javascript and contains some javascript functions for publishing data to a facebook feed. Put

{% show_connect_button %}

wherever you want a 'Connect with facebook' button.

Django has a builtin LOGIN_REDIRECT_URL setting that django-facebookconnect uses when a user logs in with facebook. By default a user will be redirected to '/accounts/profile'. If you want this to be something different, set LOGIN_REDIRECT_URL in your settings file.

There is also a management command called 'installfacebooktemplates'. This command loads special facebook templates from a directive in your settings file called FACEBOOK_TEMPLATES and stores the reference ids in the db. You can get at those templates through the FacebookTemplate model. See the settings.EXAMPLE.py file for more details on how to define facebook templates.

If you feel like working on this project contact Ryan Mark ( ryan at bariserver dot com )

Comment by GrantinP...@gmail.com, Apr 3, 2009

Could you tell me if there is any known problems in applying this app to the Pinax project? This is very cool and can be a deal breaker for many social applications.

Cool work, thanks

Grant

Comment by joseph.w...@gmail.com, Apr 6, 2009

I, too, am looking at using / integrating it into Pinax -

Joe

Comment by marko.dv...@gmail.com, Apr 8, 2009

There are pieces missing in this tutorial for FBC to work I guess.

I think my django settings part is done fine. On a Facebook Utils side under Connect I have:

Connect URL: http://127.0.0.1/fbconnect/ Account Reclamation URL: http://127.0.0.1/accounts/login/ (django profiles)

The rest is left blank.

Front page renders fine and I see connect button and JS code at the end of the source.

However I had to put {% load facebook %} in the base.html in order to get {% initialize_facebook_connect %} working.

I also got error that "auth_login" url not found. This url name is used twice in facebookconnect/views.py. I changed that to simple redirect "/fbconnect". Hence, and after clicking FB Connect icon nothing happen. Any help would be appreciated.

Comment by Jim.mixt...@gmail.com, Apr 17, 2009

Just wanted to let you know that there is another facebook connect app that is somewhat hard to use.

I'm trying this on out (yours), and I'm very reassured to see it is getting some development and attention. if it works out I'll probably be submitting patches :)

Comment by Jim.mixt...@gmail.com, Apr 22, 2009

@marko.dvornik I am also getting the auth_login error, I'll file a bug report

Comment by Jim.mixt...@gmail.com, Apr 22, 2009

I think I may have uncovered the reason for the auth_login error!

If you create an admin account during syncdb that user has no facebook_profile, but if you just check if the user is logged in (in a view) and then assume then have a facebook_profile since all users are given one on signup you get a does not exist error. The facebookconnect middleware then redirects you to the /facebook/login page

Comment by Jim.mixt...@gmail.com, May 22, 2009

I have successfully integrated this into pinax :)

just a heads up for you all :)

Comment by sharan...@gmail.com, May 28, 2009

Hi, did any of you figure out the reason for the auth_login error, there is no named entry 'auth_login' in the urls.py. Help!!

Comment by vmall...@gmail.com, Jun 4, 2009

@Jim.mixtake

Was there anything different that you needed to do from the installation procedure above to get it integrated into pinax. I was able to run syncdb but Im getting TemplateSyntax? errors with the template tags {% initialize_facebook_connect %} and {% show_connect_button %}

Comment by Jim.mixt...@gmail.com, Jun 25, 2009

@vmallory nope everything as normal... however you probably forgot to add {% load facebook %} to the top of your template ;)

Comment by atot...@gmail.com, Jun 25, 2009

@Jim.mixtake I'm stil; getting the same errors as vmallory. I tried adding {% load facebook %}, but it says: "'facebook' is not a valid tag library: Could not load template library from django.templatetags.facebook, No module named facebookRequest Method: GET"

Comment by rileycr...@gmail.com, Jun 26, 2009

Can anyone help me understand how to wire up the views?

I have installed this app and pyfacebook and everything is working (i.e. my users have a facebook_profile hanging off of them).

I'm not sure how to get the login forms/templates wired up. I have copied the facebookconnect/urls into my projects urls.py --- and this allows me to see the facebook connect button on my page --- however when I click on it it takes me to http://mysitenotshown.com/setup?next=/accounts/profile/

Can anyone provide guidance for using the installed facebookconnect/urls.py and templates etc?

Thanks!

Comment by rileycr...@gmail.com, Jun 26, 2009

@Jim.mixtake You need to {% load facebook_tags %}, NOT {% load_facebook %}. You can see this if you look in the templatetags directory....there is a file called facebook_tags.py

I hope this helps.

Comment by rileycr...@gmail.com, Jun 26, 2009

I got the urls fixed (read the "issues" section at the top of this page). Now I'm having a problem when the user tries to link his account on facebook with the django account. I keep getting the error

Warning at /facebook/setup

Out of range value adjusted for column 'facebook_id' at row 1

Any thoughts?

Comment by selforga...@gmail.com, Jun 28, 2009

Can I use django-facebookconnect to send a status update from an external site back to facebook?

I'm building a website (not a facebook app). Say a user log in to my site using his facebook login. I want to send updates like "Tom has donated $10 on externalsite.com" back to the user's facebook page.

How do I do something like that using django-facebookconnect?

Comment by lundstro...@gmail.com, Jul 21, 2009

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?

Comment by Jim.mixt...@gmail.com, Aug 13, 2009

@rileycrane in my checkout the template tags file is called facebook.py, but I would agree that facebook_tags is a better name for it :)

Comment by var...@gmail.com, Aug 28, 2009

any idea whats up with this error??

Caught an exception while rendering: Reverse for 'facebook_login' with arguments '()' and keyword arguments '{}' not found.

Original Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/django/template/debug.py", line 71, in render_node
    result = node.render(context)
  File "/usr/lib/python2.5/site-packages/django/template/defaulttags.py", line 382, in render
    raise e
NoReverseMatch: Reverse for 'facebook_login' with arguments '()' and keyword arguments '{}' not found.

and then it shows

Caught an exception while rendering: Reverse for 'facebook_login' with arguments '()' and keyword arguments '{}' not found.
1 	<form class="connect-button" name="login" method="post" action="{% url facebook_login %}"><input type="hidden" name="next" value="{{ next }}" /><input type="image" onclick="facebookConnect(this.form);return false;" src="http://static.ak.fbcdn.net/images/fbconnect/login-buttons/connect_light_large_long.gif" /></form>{% if javascript_friendly %}\{% endif %}
2 	

all I did was include show_connect_button!

this projects docs really need a bit of work, if I can figure this out, I'd be happy to make some patches!

Comment by brigh...@gmail.com, Sep 6, 2009

...I have a similar error:

TemplateSyntaxError at /

Caught an exception while rendering: Reverse for 'myproject.facebook_xd_receiver' with arguments '()' and keyword arguments '{}' not found.

Original Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/django/template/debug.py", line 71, in render_node
    result = node.render(context)
  File "/usr/lib/python2.5/site-packages/django/template/defaulttags.py", line 378, in render
    args=args, kwargs=kwargs)
  File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 254, in reverse
    *args, **kwargs)))
  File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 243, in reverse
    "arguments '%s' not found." % (lookup_view, args, kwargs))
NoReverseMatch: Reverse for 'myproject.facebook_xd_receiver' with arguments '()' and keyword arguments '{}' not found.
Comment by bobzav...@gmail.com, Sep 8, 2009

@brighama - not sure if this will help your situation, but it fixed the same error for me: be sure to add (r'^facebook/', include('facebookconnect.urls')), to your urls.py

Comment by philipp....@gmail.com, Sep 9, 2009

What connect url should i use. i did: http://localhost:8000/facebookconnect/login/ with the result that after logging in i just see a login again and that i dont get anything but an error for {% show_facebook_name user %} please help! thnx phil

Comment by project member dryanm...@gmail.com, Sep 11, 2009

Updated the docs

Comment by alex.mar...@gmail.com, Oct 25, 2009

I followed the instructions to set all this up, but every time I click on the Connect with Facebook button, I get a dialog box that pops up saying: "This site requires that you Connect with Facebook".

Then I get a 404 error because it was trying to load the url: facebook/login/<built-in function next>

I have looked all through out my code and I still have no idea where <built-in function next> is coming from.

Has anyone every come in contact with this error? What is the fix to this problem?

Comment by alex.mar...@gmail.com, Oct 25, 2009

Also, I am using python 2.6, if that helps any.

Comment by ashok...@gmail.com, Oct 25, 2009

I have followed the same procedure. when i click the facebook connect button, i see the connect with facebook window which prompts for username and password. When the details are entered, the window closes and redirects it to my application login there it says "fail, username and password invalid". Also I see the tables created when I sync the database namely "facebookconnect_facebookprofile" and "facebookconnect_facebooktemplate" are empty. Anyone has any idea what the issue is and how to resolve this issue. Any help on this will be greatly appreciated.

Comment by rajendran.dilip@gmail.com, Nov 4, 2009

Has any one fixed this problem , mentioned in the earlier posting ?

@ Comment by rileycrane, Jun 26, 2009

I got the urls fixed (read the "issues" section at the top of this page). Now I'm having a problem when the user tries to link his account on facebook with the django account. I keep getting the error

Warning at /facebook/setup

Out of range value adjusted for column 'facebook_id' at row 1

Any thoughts?

Comment by project member dryanm...@gmail.com, Nov 19, 2009

@wakeb0ardn - did you make sure to add {% facebook_js %} and {% initialize_facebook_connect %} to your page? Are you loading other javascript on the page that's throwing errors?

Comment by m.scholz...@gmail.com, Feb 12, 2010

Hey there, this script is great! But is there a way to check within the templates if a user is logged in to facebook or not? i tried {% if request.facebook.uid %} and {% if request.user.facebook_profile %} but none of them worked. any suggestions?

Cheers, Matt

Comment by dalama...@gmail.com, Feb 15, 2010

Hello, how can we check if the user is logged with facebook or regular auth system? a tag such as {% if.facebook.login user %} would be great

Comment by project member dryanm...@gmail.com, Feb 15, 2010

If you pass the request into the view, {% request.facebook.uid %} should tell you if the user is logged in with facebook. It won't tell you if the user is logged into the django auth system.

It's better to check request.user.is_authenticated and request.facebook_profile.uid to make sure the user is logged in both places. You can use request.user.facebook_profile.is_authenticated for a more authoritative answer from facebook than just testing to see that you have a logged in UID, but it involves an API call, so use it sparingly.

Comment by dalama...@gmail.com, Feb 15, 2010

thanks alot ryan! this helped alot!

Comment by dalama...@gmail.com, Feb 16, 2010

Greetings once again, I have been working on implementing django-facebookconnect to my django project, it works flawless, thumbs up for that! :) I do want to make a small modification so that I can remove the part where users bound their facebook accounts to their regular accounts. In other words, I simply want facebook accounts to stay only facebook without the option to connect their facebook accounts to their regular accounts. How can this be achieved ?

Regards

Comment by project member dryanm...@gmail.com, Feb 16, 2010

@dalamartr take a look at the setup view and template in the project. There is a 'Connect with Facebook only' option. This creates a django auth account that the user never sees. It uses the facebook uid as the username and sets an unusable password. This way you can use the bulitin django auth stuff, but have the user login with facebook only.

Copy out that template and remove the forms for the other options. Reference the setup view in your urls.py and pass it the location for your custom template. If you want to be more secure, copy the setup view as well and delete the sections pertaining to the other ways to setup an account, although this is probably unnecessary to just get your app to do what you want.

Does this help?

Comment by dalama...@gmail.com, Feb 16, 2010

Hello ryan, thanks alot for your reply! I have changed the setup.html template as such: http://pastebin.com/m7dac65c1 and the setup view such as: http://pastebin.com/m589db273

I am not sure if this is secure (it gives errors like duplicate entry with facebook UID if I type "http://127.0.0.1/facebook/setup/?next=/" while logged in as a regular django user). I must confess I am very naive with the facebook connection process.

Also I am curious if it is possible to directly register instead of letting people to click "Connect with facebook only" at the setup. (maybe like adding this direct facebook login registry under def facebook_login(request)

Regards

Regards

Comment by dalama...@gmail.com, Feb 16, 2010

I think I have done it.

For those who want to remove the linking the profile to the user and only creating user for the facebook profile, I have changed the views.py as such: http://pastebin.com/m2ee81d6

Also removed the whole setup function. Lastly remove all import setup from other files.(don't forget to remove the setup from url.py)

Let me know if there is any potential security whole with this method.

Comment by Jim.mixt...@gmail.com, Mar 18, 2010

Another note... If you're Trying to test and debug facebook connect on your localhost you'll have to set (on facebook)

Connect URL: http://localhost.localhost Base Domain: localhost.localhost

and add this to the top of your /etc/hosts file (on unix os's):

127.0.0.1 localhost.localhost

more details: http://ardentdev.com/no-facebook-connect-cookies-for-localhost-development/

Comment by imnotric...@gmail.com, Apr 8, 2010

If you get NoReverseMatch? for facebook_xd_receiver errors when you run the unit tests it is related to http://recurser.com/articles/2009/11/10/noreversematch-custom-registration-templates-break-django-unit-tests/

Just change facebookconnect/templates/facebook/js.html to have: {% url facebook_xd_receiver as facebook_xd_receiver %}{{ facebook_xd_receiver }}

Messy, but hopefully it'll get fixed in future django versions.

Comment by diegue...@gmail.com, Apr 29, 2010

This works perfect! but i have a question, this work with Single sign on?

Comment by hjrnu...@gmail.com, Jun 25, 2010

Hi guys! I've managed to get this working but I have a problem nonetheless... After I get to see the Profile view - the original that comes with the project, I switch to my main page and everything OK. However, if I change to yet another view, I get a 453 error when the middleware calls getLoggedInUser()... Any thoughts on why this might happen?

Comment by landrece...@gmail.com, Sep 12, 2010

Hi guys,

But i got this error when i try "python manage.py installfacebooktemplates" ==> facebook.FacebookError?: Error 100: action_links-href URL is not properly formatt ed

Any ideas? Thanks in advance

Comment by ColeWasH...@gmail.com, Nov 17, 2010

When I attempt to click a Connect with facebook button, I get this error: "Forbidden (403) CSRF verification failed. Request aborted."

I added a csrf_token to the connect button's template, but I don't know what else to change.

Also, what should my Site URL (used to be Connect URL) be in the Facebook application settings?

Comment by truongth...@gmail.com, Dec 30, 2010

Hi every body, I have same proble like ColeWasHere?. Please help me solve it.

Thanks

Comment by janne.vi...@gmail.com, Feb 8, 2011

Hi there!

I had some problem as ColeWasH and truongth and I successed to get it work using csrf_migration_helper.py which comes with django/extras. It checked which source files needed patching. Simply add {% csrf_token %} right after <form ...> tags in

facebookconnect/templates/facebook/connect_button.html line 1 and facebookconnect/templates/facebook/setup.html line 18 and 24

so that those tags come <form ...>{% csrf_token %} ....

I also noticed, that you must have base template called base.html or otherwice you get template errors. It would be wonderful, if someone could took django-facebookconnect under his wing and update documentation and code. There seems to be quite many unanswered issues.

Comment by rich.al...@gmail.com, Feb 19, 2011

I get all the way to the connect, and then connect, but when i view my profile page, get this, any ideas?

Invalid block tag: 'show_facebook_full_name', expected 'endblock' or 'endblock content'

Comment by rich.al...@gmail.com, Feb 19, 2011

also, using atest page, i am getting a logged in user, but i can only get the name, info, i can't get the friendsList, it shows up empty

Comment by nri.adi...@gmail.com, Mar 4, 2011

for those of you facing issues like the following: Caught an exception while rendering: Reverse for 'facebook_login' with arguments '()' and keyword arguments '{}' not found.

Original Traceback (most recent call last):

File "/usr/lib/python2.5/site-packages/django/template/debug.py", line 71, in render_node
result = node.render(context)
File "/usr/lib/python2.5/site-packages/django/template/defaulttags.py", line 382, in render
raise e
NoReverseMatch?: Reverse for 'facebook_login' with arguments '()' and keyword arguments '{}' not found. and then it shows

Caught an exception while rendering: Reverse for 'facebook_login' with arguments '()' and keyword arguments '{}' not found. 1 <form class="connect-button" name="login" method="post" action="{% url facebook_login %}"><input type="hidden" name="next" value="{{ next }}" /><input type="image" onclick="facebookConnect(this.form);return false;" src="http://static.ak.fbcdn.net/images/fbconnect/login-buttons/connect_light_large_long.gif" /></form>{% if javascript_friendly %}\{% endif %}

the following steps should help: - make sure u ran syncdb command (python manage.py syncdb) - make sure the app key and secret are correct..You should not be using API key and App Secret, instead use the App Id and App Secret

hope this helps someone!

Comment by guyfar...@gmail.com, May 12, 2011

Hi,

I have been using facebookconnect successfully for a few weeks. I want to extend the standard auth user with a UserProfile?? using the standard pattern, something like: http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/

As soon as I try and attach a user profile to a user at the time of creation eg:

user.save()
uprofile = UserProfile(user=user)
uprofile.save()

the users start being created as empty rows in the user table. I am wondering if I am introducing some incompatibility with the facebookconnect middleware or authentication backend?

Help much appreciated, been struggling with it for a while.

Guy.

Comment by bhasinn...@gmail.com, Apr 15, 2012

hi i have used facebookconnect to add facebook authorization into my site. it allows successfully login. Now my problem is after successfully loged in it should redirect to my successfully logged in page instead of facebook home. what settings do i need to make to do this?

Help will be appriciated..

Comment by ashish...@gmail.com, Apr 27, 2012

after adding facebookconnect to "settings.py" getting error . <code> $ python manage.py syncdb TypeError: db_type() got an unexpected keyword argument 'connection'

Creating tables ... Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_user_permissions Creating table auth_user_groups Creating table auth_user Creating table django_content_type Creating table django_session Creating table django_site Creating table blog_post Creating table blog_comment Creating table django_admin_log Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line utility.execute() File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 196, in run_from_argv self.execute(*args, **options.__dict__) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 232, in execute output = self.handle(*args, **options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 371, in handle return self.handle_noargs(**options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 91, in handle_noargs sql, references = connection.creation.sql_create_model(model, self.style, seen_models) File "/usr/local/lib/python2.7/dist-packages/django/db/backends/creation.py", line 44, in sql_create_model col_type = f.db_type(connection=self.connection) TypeError: db_type() got an unexpected keyword argument 'connection' </code>


Sign in to add a comment
Powered by Google Project Hosting