Facebook connect lets our users log in and interact with our site using their facebook accounts.
With facebook APIs, we can then do more stuff such as publishing their actions at our sites to facebook; be more social.
We've looked at opensocial, and thought that facebook connect is another option to tap.
Here's how we put together google app engine, python+pyfacebook and facebook connect together.
Get google app engine sdk and read the tutorial. We'll be using the guestbook sample.
Also, create a new facebook application (via facebook developer app), and get the api_key and secret.
Here's the main class:
# -*- coding: utf-8 -*-
import cgi, os
import datetime, Cookie, md5
import wsgiref.handlers
import facebook
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
api_key = 'fbook app code'
secret_key = 'fbook app secret'
class Greeting(db.Model):
author = db.StringProperty()
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
class MainHandler(webapp.RequestHandler):
def get(self):
greetings_q = Greeting.all().order('-date')
greetings = greetings_q.fetch(10)
template_values = {'greetings': greetings}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
class MembersHandler(webapp.RequestHandler):
def get(self): #get requests
#instantiate our pyfacebook
self.facebookapi = facebook.Facebook(api_key, secret_key)
#check for md5 values (cookie & calculated value) for verification
fbcookies = self.facebookapi.validate_cookie_signature(self.request.cookies)
if fbcookies is not None:
#get fb user
user = self.facebookapi.users.getInfo( [fbcookies['user']], ['uid', 'name', 'profile_url', 'relationship_status'])[0]
greetings_q = Greeting.all().order('-date')
greetings = greetings_q.fetch(10)
template_values = {'greetings': greetings,
'user': user,
}
path = os.path.join(os.path.dirname(__file__), 'members.html')
self.response.out.write(template.render(path, template_values))
class Guestbook(webapp.RequestHandler):
def post(self):
self.facebookapi = facebook.Facebook(api_key, secret_key)
fbcookies = self.facebookapi.validate_cookie_signature(self.request.cookies)
if fbcookies is not None:
user = self.facebookapi.users.getInfo( [fbcookies['user']], ['uid', 'name', 'profile_url', 'relationship_status'])[0]
greeting = Greeting()
greeting.author = user['name']
greeting.content = self.request.get('content')
greeting.put()
else:
pass
self.redirect('/')
def main():
application = webapp.WSGIApplication([('/', MainHandler), ('/members.html', MembersHandler),
('/sign',Guestbook)
],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()
Note the guestbook model where we will use user's facebook identity instead of google's.
Facebook sessions can be transferred from Javascript to server side codes via cookies. Look for cookies in the form of api_key_xxx, where xxx maybe user, ss, etc.
In the above code, the MembersHandler class is interesting since that's where much wrangling is done.
There's code duplication (when instantiating pyfacebook) in different classes. There should be a better way. Ideas?
We learned a lot (the session transfer, for one) during this experience and hope to try and do something similar with opensocial.
To work with facebook openapi stream, see the pyfacebook patch in gwibber.
Go see the demo. Leave some note behind!
Helpful resources:
http://wiki.developers.facebook.com/index.php/Facebook_Connect
http://bazaar.launchpad.net/~segphault/gwibber/template-facebook-stream/annotate/head%3A/gwibber/microblog/facebook.py
http://dollarmani-facebook.blogspot.com/2008/09/using-facebook-api-in-python.html
http://facebooktoolkit.codeplex.com/Thread/View.aspx?ThreadId=40599&ANCHOR
Trackback is http://myzope.kedai.com.my/blogs/kedai/236/tbping
The facebook.py you link to is a different version than what you use.
it *has* been a while. fb now has their own py lib.
should i update this tut?
