Source

filmaster-api-sample / fbapp.py

import webapp2

import pprint, json, urllib2, cgi
from urllib import urlencode
from utils import gae, fb

from filmaster_auth import FilmasterOAuthClient

import settings

import logging
logger = logging.getLogger(__name__)

class FBApp(gae.BaseHandler):
    def dispatch(self):
        self.filmaster_client = FilmasterOAuthClient(
                settings.FILMASTER_APP_KEY,
                settings.FILMASTER_APP_SECRET,
        )
        return super(FBApp, self).dispatch()

    def post(self):
        sreq = self.request.params.get('signed_request')
        if sreq:
            self.signed_data = fb.parse_signed_request(sreq, settings.FBAPP_SECRET)
        else:
            self.signed_data = {}

        if not self.signed_data.get('user_id'):
            return self.landing_page()
        out = {
                'GET': self.request.GET.items(),
                'POST': self.request.POST.items(),
                'headers': self.request.headers,
                'cookies': self.request.cookies,
                'signed_data': self.signed_data,
        }

        try:
            access_token = self.filmaster_client.facebook_login(self.signed_data.get('oauth_token'))
            # access token may be stored in session and reused in subsequent requests

            liked_films = self.filmaster_client.get('/1.1/profile/films-liked/?limit=50')
            recommended_films = self.filmaster_client.get('/1.1/profile/recommendations/?use_fb_likes')

            out['liked_films'] = [ f['title'] for f in liked_films['objects'] ]
            out['recommended_films'] = [ f['title'] for f in recommended_films['objects'] ]

        except Exception, e:
            logger.exception(e)
            out['error'] = unicode(e)

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

    def get(self):
        self.response.status = 302
        self.response.location = 'http://apps.facebook.com/%s/' % settings.FBAPP_ID

    def landing_page(self):
        auth_url = self.create_auth_url()
        out = "<script>top.location.href=%s</script>" % json.dumps(auth_url)
        self.response.headers['Content-Type'] = 'text/html'
        self.response.write(out)

    def create_auth_url(self):

        params = [
                ('client_id', settings.APP_ID),
                ('redirect_uri', self.request.url),
                ('scope', settings.FBAPP_PERMS),
        ]
        return "https://www.facebook.com/dialog/oauth?" + urlencode(params)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.