Wednesday, September 24, 2008

Setup Facebook

Welcome to the tutorial on how to integrate Google AppEngine and Facebook Applications.

This blog will cover the final step in integrating Google AppEngine and Facebook. Proceed to Facebook Developers website and set up new application.



Pick an Application name and expand the Optional Fields. For the purpose of this tutorial, I am going to use the Application Id provided by Google AppEngine as "application-id".

Here are some of the boxes you have to fill up:
Callback URL: http://application-id.appspot.com/
Canvas Page URL: pick one with more than 7 chars and this will be your application page. You can change this later if you wish but try not to.
Can your application be added on Facebook? Yes
Post-Add URL: Same as your canvas page URL
Post-Authorize URL: http://application-id.appspot.com/
Post-Authorize Redirect URL: Same as your canvas page URL

That finishes all the setup needed to integrate Google AppEngine and Facebook. This setup for Facebook is the bare minimum setup. For further details on other settings, check the developer docs, blogs and forums on Facebook.

If you have any questions or comments, please feel free to email at support@sparklextreme.com.

Setup Google AppEngine

Welcome to the tutorial on how to integrate Google AppEngine and Facebook Applications.

This blog will cover the setup procedures on the Google AppEngine. While the next blog will cover the setup procedures needed while creating the Facebook application.

First step would be to create an application in Google AppEngine. Go to http://appengine.google.com and login in using your google account or create a google account if you do not have one. Once you have logged in, click on "Create an Application".



Fill up the form with appropriate details. The "Application identifier" is a unique id to identify your application. You do not have to pick a nice or suitable name here since no one will use this to access the application.

Once the application is created, you have to update the application identifier in the app.yaml file provided in the sample guestbook application. Then you are ready to upload the application into Google AppEngine. Refer to Uploading section for help in uploading the application.

Thats all the setup needed on the Google AppEngine side.

Next: Setup Facebook

If you have any questions or comments, please feel free to email at support@sparklextreme.com.

Saturday, September 13, 2008

Using Facebook API in Python

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 AppEngine

If you have any questions or comments, please feel free to email at support@sparklextreme.com.

Thursday, September 11, 2008

Facebook Applications

Hi Everyone,

Facebook, with more than 100 million users, is a great place to publish your application and get some attention. Just like many of you out there, I just wanted to try creating an application and see how people like it. I didn't however want to put any money into making or supporting such an endeavor. Good hosting services cost a bit of money. I might consider getting one if my application becomes large and starts making some income for me.

Ok coming to the point. Google AppEngine is free, not without restrictions of course, but they are manageable. After some googling, trial and error, I managed to host an application in Google AppEngine and hook it up to Facebook.

Basic things you need to know and have if you want to do this.
1. Google AppEngine account (used to accept requests according to a waiting list, not sure how it is now)
2. Need to know Python
3. Facebook Account (dah!!)

Here is a neat application I have created. Its a small game of guessing the outcomes of future events in a stock trading type environment. Its completely running on Google AppEngine

http://apps.facebook.com/guessahead

Basically this tutorial is a easier version of the actual tutorial found at Python - Facebook Wiki

Summary of the tutorial:
1. Using Facebook API in Python
2. Setup Google AppEngine
3. Setup Facebook

If you have any questions or comments, please feel free to email at support@sparklextreme.com.