#!/usr/bin/perl
# Copyright David Leadbeater 2007 <http://dgl.cx>.
# Licensed under the same terms as Perl itself.
# A simple example of using WWW::Facebook::API within a facebook canvas.
# You will need to change the api_key, secret and app_path below to match your
# account. To get an api_key and secret, go to
# http://www.facebook.com/developers/editapp.php?new, choose "Use FBML" and
# enter a unique name for the canvas which you should put into app_path.
use strict;
use CGI;
use WWW::Facebook::API;
use Data::Dumper;
use CGI::Carp qw/fatalsToBrowser/;
my $facebook = WWW::Facebook::API->new(
api_key => "279476229e7a2df92108c458b1fe8519",
secret => "yesIUsedMyRealSecret",
app_path => "http://apps.facebook.com/randbash/",
parse_response => 1,
debug => 1,
);
sub main {
# Should also work with FastCGI (via CGI::Fast).
my $q = new CGI;
print $q->header;
redirect("Must be called in facebook canvas")
unless $facebook->canvas->in_fb_canvas($q);
my $params = $facebook->canvas->validate_sig($q);
if($params->{user}) {
# Canvas takes care of setting up a session for us, no need to call the
# auth methods.
$facebook->session_key($params->{session_key});
$facebook->profile->set_fbml( params => { markup => "MONKEY2", uid => $params->{user} } );
print "Set profile data!";
} else {
# User hasn't added app (could reject/display info/whatever)
# (Or handle later when a user is needed).
}
print "<table>";
print "<tr><td>$_</td><td>$params->{$_}</td></tr>\n" for keys %$params;
print "</table>";
print "<br>\nDone!\n";
# Clear session_key (for if running under FastCGI).
$facebook->session_key(undef);
}
sub redirect {
div_error("Please go <a href='" . $facebook->get_app_url . "'>to facebook</a>");
exit;
}
sub div_error {
print "<div style='padding: 20px'>", join("", @_), "</div>";
}
main();