Skip to main content

Social networking middleware for Facebook, Twitter, LinkedIn and Google (OpenID + Buzz)

Project description

About the middleware

ao.social is a social networking middleware that aims to provide a generic interface for multiple social networking web services (Facebook, Twitter, Google (OpenID login) and LinkedIn are currently implemented). It provides a standard WSGI middleware that adds the ao.social.user environment variable to the WSGI environment.

Note that you also need to add Beaker to your WSGI pipline, otherwise the middleware won’t be able to remember the users. It’s up to the developer to configure Beaker. The recommended setup is using secure cookies to store session data; however, other methods should work just as fine.

To use the ao.social middleware with Django, you need to add it to the WSGI pipeline just as you would with any other framework. ao.social does not provide a Django middleware. Instead, it won’t even call Django if it is not required (i.e. on the login handler pages). The recommended way of plumbing Django into the WSGI pipeline along with Beaker and ao.social is using the twod.wsgi package.

Configurung the middleware

The social middleware is a generic, stand-alone component that can be used for common social networking interactions, regardless of the web framework being used. However, it needs some configurations. To make things easier, you can store this configuration in an external settings file. For example, you can create a YAML config file like this:

login_path: /login/%s/
user_class: foomodule.models.User
facebook:
  key: your-facebook-api-key
  secret: your-facebook-api-secret
twitter:
  key: your-twitter-consumer-key
  secret: your-twitter-consumer-secret
google:
  realm: http://www.example.com/
  secret: your-google-api-secret
  callback: http://www.example.com/login/google/
linkedin:
  key: your-linkedin-consumer-key
  secret: your-linkedin-consumer-secret

Note that this is a minimal configuration. You can override some of the default middleware behavior by using extra parameters in the configuration. For more information, take a look at the documented tests and the source code.

If you do not wish to use one of the services, simply leave out that section from the config file. That way the client machinery won’t be loaded at all.

To load the config file from the file, use the PyYAML module:

>>> import yaml
>>> with open('auth.yaml', 'r') as file:
...     conf = yaml.load(confstr)

For the google login to work, the callback must be the login path for google:

>>> conf['login_path'] % 'google'
'/login/google/'

>>> conf['google']['callback']
'http://www.example.com/login/google/'

Say your downstream WSGI application is wsgi_app, you can initialize the middleware like this:

>>> from ao.social import middleware
>>> app = middleware.AuthMiddleware(wsgi_app, conf)

About the User object

The ao.social middleware will put the user object in the WSGI enviromnent. You chould supply the user class when instantiating the middleware, and the user class should be available to the middleware at that time. The base user class is available as ao.social.UserBase, but you should subclass it with the ORM model of your choice to make it persistent. A basic interface is provided, but you need to implement some additional methods. Take a look at the source code and the test for a list of methods that you need to implement. (Those are the ones that raise NotImplementedError.)

To log in a user with, say, twitter, redirect to config['login_path'] % 'twitter'. Same rule applies for LinkedIn and Google. Facebook is a little different, you need to use the XFBML tags to log in, and upon a successful login, redirect to config['login_path'] % 'facebook' so that the user’s key gets added to the Beaker session and the credentials (and tokens) get stored in the user model.

For facebook, it is enough to ping the login path (i.e. make a simple AJAX call).

To post to the user’s profile, simply use user.post(message), where message is a string or unicode that contains the message to be posted. This works for Facebook (status updates), Twitter (tweets) and LinkedIn (status updates). Google Buzz is not implemented yet, as the Buzz folks haven’t added OAuth support at the time of writing this module. It will probably be supported in the future.

Django template tags

There are some handy shortcats for Django applications. If you add ao.social to your INSTALLED_APPS, the social template library will become available. It contains the following three template tags:

{% apikey method %}

apikey returns the api key for the given method. Not available for Google.

{% liginbutton method onlogin %}

Renders a login button. For Facebook, it will render an XFBML login button. The developer is responsible for definig the XFBML namespace and initializing the Facebook Connect script in the template.

The onlogin parameter is only valid for Facebook. It is a JavaScript statement that will be executed upon successful login.

{% avatar height width %}

Displays the user’s profile picture.

For Google, the Gravatar API is used to construct a profile picture from the user’s email. LinkedIn doesn’t provide a profile picture so the avatar template tag won’t work for LinkedIn users.

Django template context processors

Add the ao.social.user template context processor to your django configuration and you’ll have the user variable available in all your templates.

TODO

  • Better test coverage

  • Add support for Google Buzz

Changelog

1.0.0 (2010-04-14)

  • First public release

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

ao.social-1.0.0.tar.gz (17.8 kB view details)

Uploaded Source

Built Distributions

ao.social-1.0.0-py2.7.egg (41.5 kB view details)

Uploaded Source

ao.social-1.0.0-py2.6.egg (41.5 kB view details)

Uploaded Source

ao.social-1.0.0-py2.5.egg (41.6 kB view details)

Uploaded Source

File details

Details for the file ao.social-1.0.0.tar.gz.

File metadata

  • Download URL: ao.social-1.0.0.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ao.social-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3b6abe020d47db3403b6353e2114028f3087332b955bc11577b9b5adcf960666
MD5 3d75ad415f11b121a050b6f056273ef4
BLAKE2b-256 6883cf81aeb8c75e00a63488eec6d7941cf13c67b526897eb29eee238fd59a32

See more details on using hashes here.

File details

Details for the file ao.social-1.0.0-py2.7.egg.

File metadata

File hashes

Hashes for ao.social-1.0.0-py2.7.egg
Algorithm Hash digest
SHA256 c47c13d63458c1f2bfcfa1c523e548c65551af9113e18dae9077faf502727440
MD5 c05c87989e57c2a1afb49da5d6f2771b
BLAKE2b-256 b6aca03e9764226a414276ecf5fd3c82cf256fd592607f4189b593b1859d81a9

See more details on using hashes here.

File details

Details for the file ao.social-1.0.0-py2.6.egg.

File metadata

File hashes

Hashes for ao.social-1.0.0-py2.6.egg
Algorithm Hash digest
SHA256 0231e85c2bdabef013f2c0243f1dd7e6f2bab4ec0703ec6db1bdf0d2afbb9928
MD5 309b19108cd55b5cf6f8f7b049840f63
BLAKE2b-256 6199215f1ff562f23160bfe9b0ad7fa4cb6ed194f4475c7c8318ff619bee8fe1

See more details on using hashes here.

File details

Details for the file ao.social-1.0.0-py2.5.egg.

File metadata

File hashes

Hashes for ao.social-1.0.0-py2.5.egg
Algorithm Hash digest
SHA256 2a92dae4b028c479a7eee791f585763c43355ad07aba0f46b58de90444edd72b
MD5 5947d0b666bfe00902c8b5bc4ff01942
BLAKE2b-256 de6a7ed149a9d692a24354bdbb38b492c67cb88d0019086b86ac1715a0d5f622

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page