An open API service indexing awesome lists of open source software.

https://github.com/mediapop/django-friendface

Getting Facebook to work for Django.
https://github.com/mediapop/django-friendface

Last synced: 2 months ago
JSON representation

Getting Facebook to work for Django.

Awesome Lists containing this project

README

        

# Django-Friendface

Django-friendface is a django application for interacting with Facebook. The
goal is to have a complete implementation Facebook's API's in a django centric
fashion and to support projects that have multiple Facebook Applications.

## Setup

INSTALLED_APPS += ('friendface',)

Each application you want to use you need to setup in the django admin.
Once saved the application will request all of its details from Facebook.

# Usage

{% load facebook %}

My view on Facebook!

### Authentication

AUTHENTICATION_BACKENDS += ('friendface.auth.backends.FacebookBackend',)

This will cause newly authenticated users to also create a regular Django user.
Django will link to FacebookUser to the Users profile as `facebook`. This means
that there needs to be a Profile on the User object.

class Profile(models.Model):
user = models.OneToOneField('contrib.auth.User')
facebook = models.OneToOneField('friendface.FacebookUser',
null=True,
blank=True)

### Facebook Middlewares

MIDDLEWARE_CLASSES += (
'friendface.middleware.FacebookApplicationMiddleware',
'friendface.middleware.FacebookDecodingMiddleware',
'friendface.middleware.DisableCsrfProtectionOnDecodedSignedRequest',
'friendface.middleware.FacebookSignedRequestAuthenticationMiddleware',
)

#### FacebookApplicationMiddleware

Matches your request path with the Facebook
applications paths and loads that application. If there's more than one
application with the same path it will pick the most exact match (i.e. the
shorter URL.) and set the `facebook_application` on the `request`.

def my_fancy_view(request):
app = request.facebook_application
response = app.request('http://www.mediapop.co')
return HttpResponse(response['shares'])

#### FacebookDecodingMiddleware

Decodes the `signed_request` and makes it accessible via `request.FACEBOOK`

#### FacebookSignedRequestAuthenticationMiddleware

Reads the `user_id` from a decoded request and signs that user in.

#### To run test suite
friendface is prepared for testing both it's Javascript and Python components.
Simply ensure that you got all Python requirements installed and that you
got a recent version of node.js with npm installed. Then simply type:

$ make test

If necessary npm will install requirements for phantom.js.

## Mobile detection

If the request has `mobile` set to `True` then the
`FacebookAppAuthMixin` will act like `MobileView` and set the current
session as Facebook mobile. This means that when the user access the
app on http://apps.facebook.com then the user will be redirected to
the bare domain of the app. The current normal workflow goes like:

- Facebook Tab app needs authorization
- Creates an authorization request and then sends the user to
http://apps.facebook.com
- If the user is using a mobile phone it'll now try to redirect to the
bare domain, otherwise display it inline according to the
configured canvas url.
- If the mobile detection middleware has been installed the redirect will
instead go to the same url as for mobile phones

To get detection install a mobile detection middleware and make sure
it marks the as `mobile = True`.

### Recommended mobile detection middleware

Install [django-mobi](https://pypi.python.org/pypi/django-mobi/) and
add it's middleware to our settings:

MIDDLEWARE_CLASSES += (
'mobi.middleware.MobileDetectionMiddleware'
)