[View Hello MiniFB.py Facebook App] - [Discuss Hello MiniFB.py] - [View Source Code]
"Hello World" + minifb.py = "Hello MiniFB.y".
You should have some working knowledge of Python and mod_python. This tutorial just helps you get a skeleton application setup and working on your webserver, but you need to take the development from there.
Some things you may want to notice so you can look for them later in the code:
tar -xzvf minifb-1.1.tar.gz tar -xzvf simplejson-1.7.1.tar.gzI just put the resulting files into /home/[userid]/lib which for me is:
/home/etienne/libNow that we have all the required software lets move onto setting things up. One important note here is that since Facebook is essentially proxing requests from the user to your webserver, your webserver has to be accessible from Facebook.com (i.e. not behind a home router somewhere)
mkdir /var/www/hellominifbThen edit your apache2.conf (or in some cases /etc/apache2/sites-available/default) and add in the necessary lines to let apache2 know how to execute Python files in that directory
# Hello MiniFB.py program for Facebook
<Directory "/var/www/hellominifb>
AddHandler mod_python .py
PythonHandler mod_python.publisher
DirectoryIndex hello.py
PythonPath "sys.path+['/home/etienne/lib']"
</Directory>
Make sure to substitute whichever directory you untarred your simplejson and minifb.py files into instead of '/home/etienne/lib' should be something like '/home/myuserid/lib' and also that you've put the appropriate directory for "/var/www" for your configuration. You may want to use a directory name more appropriate for your application than "hellominifb". At this point lets run a test to make sure that mod_python itself is working as we expect it. Create simpletest.py and include this code:
def index(req): return "<html><h1>test mod_python</h1></html>"Place this in your equivalent of /var/www/hellominifb and then visit the url using your browser. The url should roughly look like this with "yourservername" and "hellominifb" changed.
http://yourservername.com/hellominifb/simpletestYou should see something like this as output:
Also here is simpletest runnig on my server so you can see what it should look like: simpletest. At this point you know that your mod_python setup is basically working.
Step 4. Test that "import minifb" works
Copy the "Hello MiniFB.py" code (i.e. hello.py) onto your server and verify that you've setup your sys.paths correctly and that hello.py can find and import minifb/simplejson.
Here is the code you need to download: hello.py
Make sure when you copy it into your equivalent of the /var/www/hellominifb directory that the file is actually named hello.py NOT hello.py.txt And now you can try to access the basic page to verify that it by going to:
http://yourservername.com/hellominifb/You should see a page similar to this: http://keepnix.com/hellominifb/ serving from your own server. If you haven't set your DirectoryIndex to hello.py in your apache2.conf as described above you may need to access it through http://yourservername.com/hellominifb/hello If there are problems you may get an "Internal Server Error" message and you can refer to your error_logs to see the stack trace (my error_logs are located at /var/log/apache2/error_log, but it varies by configuration).
Now enter your application name and open the "Optional Fields" area of the "Add Application" page. You see a section for "Optional Fields" (the first red X marked below) which you can open to display the rest of the fields you'll need to set.
Scroll down on the above screen shot of the "Add Application" page to see the 7 pertinent red X's that are important to configure. Remember to substitute your own servername instead of keepnix and own url name if you've changed the directory.
Once you've created your appication you can view your app keys by clicking on the "See My Apps" link (located just below the "+Set Up New Application" button on the Developers Application main page).
You can see the api key and app secret in the above image marked with 2 red x's.
Step 6. Configuring hello.py
Open hello.py (in my install it is at /var/www/hellominifb/hello.py) using your favorite text editor and go to the first section (labeled "[1] app settings"). There are five required settings and one optional setting:
##################################################
# [1] app settings
_FbApiKey = ""
_FbSecret = minifb.FacebookSecret("")
_canvas_url = "http://apps.facebook.com/hellominifb"
_app_name = "Hello MiniFB.py"
_userdb = "/tmp/hellominifb.txt" # must be a file writable by the webserver
_google_analytics_id = None # optional setting
Enter the apikey and the fbsecret key from Step 4 into the first two values and also set the _canvas_url to the url you specified from Step 4. At this point your setup and install of "Hello MiniFB.py" should be complete.
Step 7. Test your install
At this point you should be able to view your application through your app's canvas url:
http://apps.facebook.com/yourappurland be able to add it to your Facebook account just like any other application. Remember if you have any problems you can check your apache error_log to see if there was an unhandled exception also you can refer to the Hello MiniFB.py Discussion Board or contact me.
I've tried to comment the hello.py code to make it straightforward to understand. Please let me know via the Hello MiniFB.py Discussion Board or contact me directoy if there are any areas that can be improved so it can be more easily understood. Also please send in any feedback / suggestions for what other features could be added to the skeleton, how this tutorial could be improved, etc.
© 2007 Steven Chow