Facebook-Graph
view release on metacpan or search on metacpan
lib/Facebook/Graph/Cookbook/Recipe1.pod view on Meta::CPAN
=head2 Step 6: Create your Facebook::Graph object.
Now we can finally start building L<Facebook::Graph> into our app.
my $fb = Facebook::Graph->new(
postback => 'http://www.yourapplication.com/facebook/postback',
app_id => 'Put Your Application ID Here',
secret => 'Put Your Application Secret Here',
);
Now you need the URL you entered in step 3, and the application ID and secret you got in step 4.
On the end of the url, add C<postback>. This could be anything really, but it needs to be separate from the Connect URL.
=head2 Step 7: Create your application's connect page.
Now we need to create the authorization redirect. This is where we tell Facebook what permissions we want. There is a complete list of permissions documented at L<http://developers.facebook.com/docs/authentication/permissions>.
If we only wanted basic permissions we can leave the C<extend_permissions> call out. But for this app let's say we want access to the user's email address and we want to be able to interact with the user's account even when they aren't online.
my $connect = sub {
my $env = shift;
my $request = Plack::Request->new( $env );
my $response = $request->new_response;
$response->redirect(
$fb
->authorize
->extend_permissions( qw(email offline_access) )
->uri_as_string
);
return $response->finalize;
};
$urlmap->map("/facebook" => $connect);
We map the subroutine we created to C</facebook> because we'll likely have other things we want to display at C</>. If we wanted to display something else at C</facebook> we could have mapped this function to C</facebook/authorize>. It really doesn't...
=head2 Step 8: Create the Facebook access token postback page.
Our connect/authorization page will redirect the user to Facebook to authorize our app. Now we need to create the page that the user will be redirected back to from Facebook. This is the C<postback> that we created in step 6.
my $postback = sub {
my $env = shift;
my $request = Plack::Request->new( $env );
# turn our authorization code into an access token
$fb->request_access_token($request->param('code'));
# store our access token to a database, a cookie, or pass it through the URL
my $uri = URI->new('http://www.yourapplication.com/search');
$uri->query_form( access_token => $fb->access_token );
my $response = $request->new_response;
$response->redirect( $uri->as_string );
return $response->finalize;
};
$urlmap->map("/facebook/postback" => $postback);
It's really stupid of us to pass our access token along the URL especially since we requested C<offline_access>. We're only doing it here to demonstrate the usage of it. If you're requesting offline access, you should keep the access token locked awa...
=head2 Step 9: Let's do something already!
So now that we finally have an access token we can start making privileged requests. That works like this:
my $search = sub {
my $env = shift;
my $request = Plack::Request->new( $env );
# display a search
my $out = '<html>
<body>
<form>
<input type="hidden" name="access_token" value="'. $request->param('access_token') .'">
<input type="text" name="q" value="'. $request->param('q') .'">
<input type="submit" value="Search">
</form>
<pre>
';
# display the results if a search is made
if ($request->param('q')) {
$fb->access_token( $request->param('access_token') );
my $response = $fb->query
->search($request->param('q'), 'user')
->limit_results(10)
->request;
$out .= eval{$response->as_json};
if ($@) {
$out .= 'ERROR: '.$@->[1];
}
}
# close everything up
$out .= '
</pre>
</body>
</html>
';
my $response = $request->new_response;
$response->status(200);
$response->content_type('text/html');
$response->body($out);
return $response->finalize;
};
$urlmap->map("/search" => $search);
=head2 Step 10: Start the application and let's test this puppy out.
On your server (the one that www.yourapplication.com points to) run the following command (assuming you're in the folder with app.psgi).
sudo plackup --port 80 app.psgi
Now we point our browser to:
http://www.yourapplication.com/facebook
( run in 0.687 second using v1.01-cache-2.11-cpan-39bf76dae61 )