Skip to main content

Easy Facebook integration for Django.

Project description

ECL Facebook

ECL Facebook is a Facebook library for Python 2.7+. It makes the Facebook API a joy to use and has built-in integration for Django and Flask.

If you have an issue to report or a feature request, add it at


ECL Facebook is on PyPi, and we recommend installing via pip

$ pip install ecl-facebook


If you’d like to use ECL Facebook for a stand alone application (e.g., in a script you’re writing to download your tweets), you’ll need to set the environment variables FACEBOOK_KEY, FACEBOOK_SECRET, FACEBOOK_REDIRECT_URL, and FACEBOOK_SCOPE with the values appropriate for your Facebook application.

export FACEBOOK_KEY="256064624431781"
export FACEBOOK_SECRET="4925935cb93e3446eff851ddaf5fad07"
export FACEBOOK_SCOPE="email"

If you’re only interested in integration with Django, read the section below.


We’ve made authentication very simple. Probably too simple, to be honest.

>>> from ecl_facebook.settings import DIALOG_URL

After opening this URL in your browser and allowing the application, you’ll be redirected to a page with a URL similar to the following.

You’ll need to paste this code in the code variable below.

>>> from ecl_facebook import Facebook
>>> code = "AQDOvI5wqlwNXQ6AK9jepHW4LUKboJk7v9yLGeaFNCDCs1hchWpCYoqDF0FZFLS03YOZJ1lLhrzQrQ7PNWD2iiZZ6IBaW0KG6255_e3prYu60QZd6_IOIiC1z0U3w2SWJDiq_rtD0KQtcJk__YvZa1XSicZA5fnyEtEZBE3XzNpEgzp1fZZ8HEeQCrqazGjUNjU"
>>> facebook = Facebook()
>>> data = facebook.oauth.access_token(code=code)
>>> data
<Objectifier#dict access_token=str expires=str>

Congratulations, you have successfully authenticated with Facebook. data is an Objectifier object which should contains your token and its expiration time.

To call the API, use your newly-acquired access token and access token secret.

>>> facebook = Facebook(data.access_token)
<Objectifier#dict username=unicode first_name=unicode last_name=unicode verified=bool name=unicode locale=unicode gender=unicode email=unicode link=unicode timezone=int updated_time=unicode id=unicode>

So, yeah. That’s it. Be fruitful and multiply.

Integrating with Django

What we did above is easy. For Django projects, we’ve made it even easier. In your views file,

from django.contrib.auth import authenticate, login
from django.http import HttpResponseRedirect

from ecl_facebook.django_decorators import facebook_begin, facebook_callback
from ecl_facebook import Facebook

from .models import User

# ...

def oauth_facebook_begin(request):

def oauth_facebook_complete(request, access_token, error):
    if error is None:
        facebook = Facebook(token)
        fbuser =
        user, _ = User.objects.get_or_create(, defaults={
            'access_token': access_token})
        user = authenticate(
        login(request, user)
        return HttpResponseRedirect(reverse('home'))
        # handle authentication exception

Of course, you’ll need to have a URL with the name home defined in your URLs file. Now, add these values to your settings.

# The User model that you'll be using to authenticate with Facebook.

    # ...

FACEBOOK_KEY = "256064624431781"
FACEBOOK_SECRET = "4925935cb93e3446eff851ddaf5fad07"

There’s also setting called FACEBOOK_CSRF_TOKEN_REQUIRED, which is True by default. We don’t suggest you change this one unless you have a really good reason.

Then map the above views in your

# ...

urlpatterns = patterns('app.views',
    # ...
    url(r'^oauth/facebook/begin$', 'oauth_facebook_begin'),
    url(r'^oauth/facebook/complete$', 'oauth_facebook_complete'),

You’re done. Oh, you might also want to add some fields for storing the Facebook-related fields in your user model.


  • Decorators for other popular Python frameworks.

  • More comprehensive test suite.

  • More users!

Contributing, feedback, and questions

Indices and tables

  • genindex

  • modindex

  • search

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ecl_facebook-1.4.1.tar.gz (9.0 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page