Fun with Python and Javascript

ontheplates.com - mybucket.co - Didip Kerabat

15 notes

Foursquare OAuth2 Tornado Mixin

The GitHub repo is here. Use this mixin to interact with Foursquare OAuth2 API.

It’s very similar to Tornado’s auth.FacebookGraphMixin. Below is sample code on how to authenticate to Foursquare:

class FoursquareLoginHandler(LoginHandler, FoursquareMixin):
    @tornado.web.asynchronous
    def get(self):
        if self.get_argument(“code”, False):
            self.get_authenticated_user(
                redirect_uri=’/auth/foursquare/connect’,
                client_id=self.settings[“foursquare_client_id”],
                client_secret=self.settings[“foursquare_client_secret”],
                code=self.get_argument(“code”),
                callback=self.async_callback(self._on_login)
            )
            return

        self.authorize_redirect(
            redirect_uri=’/auth/foursquare/connect’,
            client_id=self.settings[“foursquare_api_key”]
        )

    def _on_login(self, user):
        # Do something interesting with user here. See: user[“access_token”]
        self.finish()

Similar to Tornado’s other API setup, don’t forget to assign foursquare_client_id and foursquare_client_secret into Application constructor. See below:

import tornado.web

class Application(tornado.web.Application):
    def __init__(self):
        tornado.web.Application.__init__(self, routes(), **dict(
            foursquare_client_id    = options.foursquare_client_id,
            foursquare_client_secret= options.foursquare_client_secret,
        ))

Filed under python tornado foursquare oauth2

  1. didip posted this