Log in

tipfy.ext.auth.twitter

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

You can use it to provide Twitter login for a site or to fetch Twitter feeds.

Try our Twitter auth demo (source here).

Twitter authentication

To authenticate with Twitter, register your application with Twitter at http://twitter.com/apps. Set the callback url to [your-domain].com/auth/twitter/. Then copy your Consumer Key and Consumer Secret to config.py:

config['tipfy.ext.auth.twitter'] = {
    'consumer_key':    'XXXXXXXXXXXXXXX',
    'consumer_secret': 'XXXXXXXXXXXXXXX',
}

When your application is set up, you can use the TwitterMixin to authenticate the user with Twitter and get access to their stream. You must use the mixin on the handler for the URL you registered as your application's Callback URL. For example:

from tipfy import RequestHandler, abort
from tipfy.ext.auth.twitter import TwitterMixin
from tipfy.ext.session import CookieMixin, SessionMiddleware

class TwitterHandler(RequestHandler, CookieMixin, TwitterMixin):
    middleware = [SessionMiddleware]

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

        return self.authorize_redirect()

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

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

CookieMixin and SessionMiddleware are needed because the handler sets a temporary cookie during authentication.

The user object returned by get_authenticated_user() includes the attributes 'username', 'name', and all of the custom Twitter user attributes describe at http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-users%C2%A0show in addition to 'access_token'. You should save the access token with the user; it is required to make requests on behalf of the user later with twitter_request().

Twitter API requests

To make Twitter API requests, you use twitter_request(). It fetches the given API path, e.g., '/statuses/user_timeline/btaylor'.

The path should not include the format (we automatically append '.json' and parse the JSON output).

If the request is a POST, post_args should be provided. Query string arguments should be given as keyword arguments.

All the Twitter methods are documented at http://apiwiki.twitter.com/Twitter-API-Documentation.

Many methods require an OAuth access token which you can obtain through authorize_redirect() and get_authenticated_user(), as described above. The user returned through that process includes an 'access_token' attribute that can be used to make authenticated requests via this method.

Here's an example:

from tipfy import RequestHandler, Response
from tipfy.ext.auth.twitter import TwitterMixin
from tipfy.ext.session import CookieMixin, SessionMiddleware

class TwitterHandler(RequestHandler, CookieMixin, TwitterMixin):
    middleware = [SessionMiddleware]
    
    def get(self):
        return self.twitter_request(
            '/statuses/update',
            post_args={'status': 'Testing Twitter Mixin'},
            access_token=user['access_token'],
            callback=self._on_post)

    def _on_post(self, new_entry):
        if not new_entry:
            # Call failed; perhaps missing permission?
            return self.authorize_redirect()

        return Response('Posted a message!')

Extension Reference


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