Facebook API : Exporting your friends birthdays into vCards format

The following sample Facebook desktop application exports your friends birthdays in a vCard file format. This file is suitable to be imported into your GMail contacts for example.

Here is the script. It’s pretty simple :

#! /usr/bin/perl

use strict;
use warnings;

use WWW::Facebook::API;
use Text::vCard::Addressbook;
use Date::Parse;

my $client = WWW::Facebook::API->new(
	desktop => 1,
	api_key => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
	secret  => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
);

my $token = $client->auth->create_token;

my $url = $client->get_login_url( auth_token => $token, req_perms => 'friends_birthday' );
system "firefox", $url;
sleep 15;

$client->auth->get_session($token);
my $response = $client->fql->query(query => 'select first_name, last_name, birthday from user where uid in (select uid2 from friend where uid1=me())');

my $ab = text::vcard::addressbook->new;
# processes each contact and creates a vCard for it {{{
foreach my $contact (@$response) {
	my $vc = $ab->add_vcard;

	my $fn = $contact->{first_name} || "";
	my $ln = $contact->{last_name} || "";
	$ln = uc $ln;
	my $bd = $contact->{birthday} || "";
	$bd =~ s/,//g;

	$vc->fullname($fn . " " . $ln);
	if ($bd) {
		my ($ss,$mm,$hh,$day,$month,$year,$zone) = strptime($bd);
		$vc->bday(join("-", (1900 + ($year || " "), $month + 1, $day)));
	}
}
# }}}
print $ab->export();
$client->auth->logout;

This is intended as a sample code for using the WWW::Facebook::API perl module (there are way too few working examples out there). Let me know if you need complementary explanations about anything.


The only thing I really need to figure out now is how to proceed if I ever wanted to distribute this code without giving away the application secret. There is a rather good general explanation of the authentication process of a facebook desktop application, but I couldn’t find a working sample code to get started with. If you do know how to do that, please share in the comments.

2 thoughts on “Facebook API : Exporting your friends birthdays into vCards format”

Comments are closed.