Monday, February 28, 2011

Use a perl script and the Graph API to access your own Facebook account

You want to create a script to read messages and post status updates to your own Facebook account, but you find the official documentation confusing and you aren't sure where to start. Search no more because here you'll find the easiest way to do just this.

The process is actually fairly simple, and recent updates to the Graph API explorer have made it dead easy:
  1. If you haven’t done so, visit your applications page on Facebook to create a new application. The default settings are fine, so you can leave them unchanged.
  2. Visit the Graph API explorer and on the top right corner choose your application. Click on the “Get access token” button, select the permissions you want to give to your application (depending on what you want to do you’ll probably need offline_access, read_stream and publish_stream), then confirm to get your access token. Copy and paste this value into the example code bellow.
  3. That’s it! Now you can go and call any of the allowed methods from the Facebook Graph API. Also make sure to understand the difference between /home, /posts, /feed, and /statuses.
The following example code shows how to read posts from your news feed and post a new message to your own wall:
#!/usr/bin/perl

use strict;
use warnings;
use open qw(:std :utf8);
use LWP::Simple;
use YAML::Tiny;
use JSON;
use URI;
use utf8;

my $access_token = 'YOUR_ACCESS_TOKEN';

# Fetch your News Feed from Facebook
my $resp = graph_api('me/home', { access_token => $access_token });
for my $post (@{ $resp->{data} }) {
  # do something with each $post
  print Dump($post);
}

# Publish a new message to your own wall
graph_api('me/feed', {
  access_token => $access_token,
  message      => 'Hello World! I’m posting Facebook updates from a script!',
  link         => 'http://qscripts.blogspot.com/2011/02/post-to-your-own-facebook-account-from.html',
  picture      => 'http://navarroj.com/stuff/share-icon-128x128.png',
  name         => 'Post to your own Facebook account from a script',
  caption      => 'qscripts.blogspot.com',
  description  => 'You want to create a script to read messages and post status updates to your own '
                . 'Facebook account, but you find the official documentation confusing and '
                . 'you aren’t sure where to start. Search no more because here you’ll find '
                . 'the easiest way to do just this.',
  method       => 'post'
});

exit 0;

sub graph_api {
  my $uri = new URI('https://graph.facebook.com/' . shift);
  $uri->query_form(shift);
  my $resp = get("$uri");
  return defined $resp ? decode_json($resp) : undef;
}

Note that, to be able to make secure https requests, you'll also need to have installed either Net::SSL or LWP::Protocol::https. Finally, if everything works, you should be able to see something like this posted on your wall.


You’re welcome.

6 comments:

Robert said...

Is there an easy way to add an attachment?

juan antonio said...

What do you mean by an attachment? You can include a link and a picture as shown in the example. See the post API documentation for the full set of parameters that you can supply.

juan antonio said...

Also have a look at this question.

Robert said...

Thanks. I got it.

system("curl -k -F access_token='123456789' -F \"message=$message\" -F 'picture=\@www/images1/$file' https://graph.facebook.com/155013054552698/photos");

Bob said...

Very cool, thanks for this post / script! I am doing a blog post on this and you have inputted something really useful. Thanks again

Corneliu said...

Thanks a lot!!