Log in

tipfy.ext.auth.facebook

This extension allows you to authenticate and make Facebook API requests on behalf of users on App Engine.

You can use it to provide Facebook login for a site or to make requests for Facebook apps.

Try our Facebook auth demo (source here).

Facebook authentication

To authenticate with Facebook, first register your application with Facebook at http://www.facebook.com/developers/apps.php. Then copy your API Key and Application Secret to config.py:

config['tipfy.ext.auth.facebook'] = {
    'api_key':    'XXXXXXXXXXXX',
    'app_secret': 'XXXXXXXXXXXX',
}

Also, go to the "Connect" section of your application settings in Facebook, and set the value for "Connect URL". It should be the URL in your site that is called after a successful authentication.

When your application is set up, you can use the FacebookMixin like this to authenticate the user with Facebook:

from tipfy import RequestHandler, abort
from tipfy.ext.auth.facebook import FacebookMixin

class FacebookHandler(RequestHandler, FacebookMixin):
    def head(self, **kwargs):
        """Facebook pings this URL when a user first authorizes your application."""
        return Response('')

    def get(self):
        if self.request.args.get('session', None):
            return self.get_authenticated_user(self._on_auth)

        return self.authenticate_redirect()

    def _on_auth(self, user):
        if not user:
            abort(403)

        # Set the user info in the session.
        # ...

The user object returned by get_authenticated_user() includes the attributes 'facebook_uid' and 'name' in addition to session attributes like 'session_key'.

If your application will make requests on behalf of the user later with facebook_request(), you should save the session key with the user.

Facebook API requests

If you're building a Facebook app, you need to use facebook_request() to makes a Facebook API REST request.

We automatically include the Facebook API key and signature, but it is the callers responsibility to include 'session_key' and any other required arguments to the method.

The available Facebook methods are documented here: http://wiki.developers.facebook.com/index.php/API

Here is an example for the stream.get() method:

from tipfy import RequestHandler, redirect
from tipfy.ext.auth.facebook import FacebookMixin
from tipfy.ext.jinja2 import Jinja2Mixin

class MainHandler(RequestHandler, Jinja2Mixin, FacebookMixin):
    def get(self):
        self.facebook_request(
            method='stream.get',
            callback=self._on_stream,
            session_key=self.current_user['session_key'])

    def _on_stream(self, stream):
        if stream is None:
           # Not authorized to read the stream yet?
           return redirect(self.authorize_redirect('read_stream'))

        return self.render_response('stream.html', stream=stream)

Extension Reference


Powered by Moe. Yeah, the name is Moe. Powered by Google App Engine