Welcome to the tutorial on how to integrate Google AppEngine and Facebook Applications.
For this tutorial I am going to modify the guestbook application provided with Google AppEngine. Download the Python Wrapper for Facebook
here and place it in the same folder where guestbook.py resides.
This tutorial assumes that you have basic knowledge of how to create an application using Google AppEngine. If not, please read the
Getting Started Guide and continue this tutorial after you have acquainted with AppEngine.
Since we are not going to use the Google user management, change the DB Model:
class Greeting(db.Model):
author = db.StringProperty() # change the author to a string
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
I created my Facebook Application in FBML mode (if you are not sure what this is, don't worry about it now). This means that all the requests are post requests. Therefore, change all the Get request handlers to Post handlers.
class MainPage(webapp.RequestHandler):
def post(self):
# Both these keys are provided to you when you create a Facebook Application.
api_key = '<your_api_key>'
secret_key = '<your_secret_key>'
# Initialize the Facebook Object.
self.facebookapi = facebook.Facebook(api_key, secret_key)
# Checks to make sure that the user is logged into Facebook.
if self.facebookapi.check_session(self.request):
pass
else:
# If not redirect them to your application add page.
url = self.facebookapi.get_add_url()
self.response.out.write('<fb:redirect url="' + url + '" />')
return
# Checks to make sure the user has added your application.
if self.facebookapi.added:
pass
else:
# If not redirect them to your application add page.
url = self.facebookapi.get_add_url()
self.response.out.write('<fb:redirect url="' + url + '" />')
return
# Get the information about the user.
user = self.facebookapi.users.getInfo( [self.facebookapi.uid], ['uid', 'name', 'birthday', 'relationship_status'])[0]
# Display a welcome message to the user along with all the greetings.
self.response.out.write("<html> <body>")
self.response.out.write('Hello %s,<br>' % user['name'])
self.response.out.write('Welcome to guestbook in facebook.<br>')
self.response.out.write('See all the entries in your guestbook below.<br><br>')
greetings = db.GqlQuery("SELECT * "
"FROM Greeting "
"ORDER BY date DESC LIMIT 10")
for greeting in greetings:
if greeting.author:
# change the greeting.author.nickname() to just greeting.author
# since we change the author from a user property to a string property
self.response.out.write('<b>%s</b> wrote:' % greeting.author)
else:
self.response.out.write('An anonymous person wrote:')
self.response.out.write('<blockquote>%s</blockquote>' %
cgi.escape(greeting.content))
self.response.out.write("""
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>""")
Now your main page is ready. Now to handle the sign action. In the sign action, we have to do the basic Facebook checks instead of the google user check and then just insert the greeting into the table.
class Guestbook(webapp.RequestHandler):
def post(self):
# Both these keys are provided to you when you create a Facebook Application.
api_key = '<your_api_key>'
secret_key = '<your_secret_key>'
# Initialize the Facebook Object.
self.facebookapi = facebook.Facebook(api_key, secret_key)
# Checks to make sure that the user is logged into Facebook.
if self.facebookapi.check_session(self.request):
pass
else:
# If not redirect them to your application add page.
url = self.facebookapi.get_add_url()
self.response.out.write('<fb:redirect url="' + url + '" />')
return
# Checks to make sure the user has added your application.
if self.facebookapi.added:
pass
else:
# If not redirect them to your application add page.
url = self.facebookapi.get_add_url()
self.response.out.write('<fb:redirect url="' + url + '" />')
return
# Get the information about the user.
user = self.facebookapi.users.getInfo( [self.facebookapi.uid], ['uid', 'name', 'birthday', 'relationship_status'])[0]
greeting = Greeting()
greeting.author = user['name']
greeting.content = self.request.get('content')
greeting.put()
self.redirect('/')
Now our coding is done for this example. This is a simple example which can easily be expanded to handle complex situations.
Next:
Setup Google AppEngineIf you have any questions or comments, please feel free to email at support@sparklextreme.com.