Source

filmaster-api-sample / samplewebapp.py

Full commit
import webapp2
import pprint, json, urllib2, cgi

from utils import gae
from filmaster_auth import FilmasterOAuthClient

import settings

import logging
logger = logging.getLogger(__name__)

class FilmasterClientMixin(object):
    def dispatch(self):
        self.filmaster_client = FilmasterOAuthClient(
                settings.FILMASTER_APP_KEY,
                settings.FILMASTER_APP_SECRET,
        )
        return super(FilmasterClientMixin, self).dispatch()

class SampleApp(FilmasterClientMixin, gae.BaseHandler):
    def authorize(self):
        request_token = self.filmaster_client.fetch_request_token()
        self.session['request_token'] = str(request_token)
        authorize_url = self.filmaster_client.get_authorize_url(request_token, callback_url = self.request.host_url+'/auth-cb/')
        self.response.status = 302
        self.response.location = authorize_url

    def get(self):
        access_token = self.session.get('access_token')
        if access_token is None:
            return self.authorize()
        self.filmaster_client.set_access_token(access_token)
        profile = self.filmaster_client.get('/1.1/profile/')
        out = pprint.pformat(profile)

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write(out)

    class AppError(Exception):
        pass

class SampleAppAuth(FilmasterClientMixin, gae.BaseHandler):
    def get(self):
        request_token = self.session['request_token']
        verifier = self.request.params.get('oauth_verifier')
        access_token = self.filmaster_client.fetch_access_token(request_token, verifier)
        self.session['access_token'] = str(access_token)
        self.response.status = 302
        self.response.location = '/'