Writing Facebook web applications in Perl (part 1)

| 7 Comments

So you want to write some web application for this controversial but universally-used social network. The good part of it is that you can use any language you want for this task, as Facebook applications reside on a server of your choice and you just need to configure the application on FB site to map to your URL.

The most commonly used languages to extend Facebook are FBML (a markup language you enter directly in the web page you create) and PHP. We use our favourite, that is to say Perl.

This short blog post shows how to create something which is very basic. I hope, however, that this post will become just part 1 of a series of posts where we can grow more complex applications.

First of all, you need to perform some actions on Facebook.

There's a tiny "Developer" link at the bottom of any given Facebook web page, which sits together with other links. Click on it and you're on the developer introduction page. Click on the big red button named "Start building for your site": you'll be asked to enter your web site URL and to upload an HTML file on it (for verification purposes). It's probably wise to dedicate a virtual domain to your Facebook application.

Now click on the "Developer dashboard" link on the yellow zone of the page, accept the Developer application, and you're now all set to manage your application (creating other ones, destroying the ones you have, etc..). Your application(s) is now manageable at:

http://www.facebook.com/developers/

Looking into this section, you'll see the API keys and other data related to it. The example code is (doh!) in PHP, but you'll find a Perl example below in this article.

Before anything else, click Edit Settings inside your application and choose the Canvas tab. The first two fields of this tab are the parts of the URL you want to appear on Facebook ( Canvas Page URL ) and the application URL on your web server ( Canvas Callback URL ): Facebook maps the first URI to the second, so this needs to be configured. In this same tab of the configuration, also set the Render Method to FBML: our examples need this setting to work properly.

Your web site is now "connected" to Facebook. and you (and users) can now access the application via:

http://apps.facebook.com/your_canvas_page_name/

That's it for the language-agnostic part of the configuration. There are actually tons of other configuration options, you can have a look at them and/or read the documentation. We'll likely use some of them in future articles.

The basic shape of your Perl application is as simple as:

use Modern::Perl;
use CGI::Simple;
my $q = CGI::Simple->new();

print $q->header();
print "Hello Facebook";

By accessing the Canvas Page URL, you'll see Hello Facebook inside all the usual Facebook page decorations.

Let's now try with something just a bit more complex, which aims to show the names of the friends of the user browsing the application. We need a CPAN module, the handy WWW::Facebook::API by David Romano. This module provides a convenient Perl wrapping around Facebook API.

use Modern::Perl;
use CGI::Simple;
use WWW::Facebook::API;
#use Data::Dump qw/dump/;

my $q = CGI::Simple->new();
my $fb = WWW::Facebook::API->new(
    desktop => 0,
    api_key => 'your_api_key',
    secret  => 'your_secret_key',
    query   => $q,
);

# UTF-8 doesn't seem to play well with Facebook, so let's stick to utf8
print $q->header(
    -charset    => 'ISO-8859-1',
);

# FBML to show application name, etc
print '<fb:header/><div>';

# This prints proper FBML code to redirect to Application Add page
# if app was not added
$fb->require_add() and exit;

# Get canvas parameters (current user, etc...)
my $params = $fb->canvas->validate_sig();
# print dump $params; # Have a look at these

# Set session key to the one passed by Facebook
$fb->session_key( $params->{session_key} );

print '<h3>First Facebook Application</h3><br />';

# Get friends of logged in user (returns a ref to array of uids)
my $user_friends = $fb->friends->get();

# Get the fields we need (name) of the user's friends
my $friends_info = $fb->users->get_info(
    uids    => $user_friends,
    fields  => [qw/ name /],
) // [ ];

# Print this out "nicely"
print '<h4>Your friends (' . @$friends_info . ')</h4><br />';
print '<ul><li>';
print join '</li><li>', sort map { $_->{name} } @$friends_info;
print '</li></ul>';

print '</div>';

# Drop session key (in case we are in a persistent environment)
$fb->session_key(undef);

This code will provide the logged in user, once he/she added (authorized) the application (not needed if the user created it, but try with a Facebook account different from yours...), with an alphabetically sorted list of his/her friends.

Well, enough for today. ;-)

7 Comments

Looking forward to the rest of the series! :-)

Phillip.

Thanks for the article! I hope there will be a part 2+

Hi!

I'm really happy you liked the article. I plan to have a series of maybe 4 or 5 of these, but I think I'll be able to make one every two weeks and not every week as I would like to. Time is short, as usual. ;-)

Thanks again,
Michele.

Hi,
Thanks for a nice article.
But i have one question.
Usually facebook calls index.php in case of php.
But in which root file i should provide this given perl sample code.
Thanks in advance.
Kind Regards,
Mohan

Hello!

I think Facebook just calls the URL you give, that is:

http://apps.facebook.com/your_canvas_page_name/

You then just configure the proper DirectoryIndex.

Michele.

This script does not work. HUGE thumbs down to you for publishing this without testing it first. For starters, you can't send a redirect using the require_add function after you've printed charset headers. Not to mention this script contains a syntax error.

I have yet to see a single Perl script online that successfully does anything with Facebook Connect. Such a disappointment.

Hello!

In 2009 this script was tested and did indeed work, so no thumbs down for that, thank you. :-)

If in the meanwhile the Facebook infrastructure changed somehow and this woes not work anymore I'm sorry, but I'd be happy to accept patches.

I'm not improving it myself, as I lost interest in Facebook programming in the meanwhile.

Cheers,
M.

Leave a comment

About this Entry

This page contains a single entry by Michele Beltrame published on November 26, 2009 11:52 AM.

Associazione Perl.It: le tessere was the previous entry in this blog.

Perl: programmazione postmoderna e filosofia open source is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 5.14-en