In the last post I explained how to post to a FB wall (yours or a friend) via a simple FB App, and with the FB PHP SDK 3. In this post I like to show the same but from the Command Line (CLI) and with Perl.

featured image

Purpose: find within your friends and liked pages, the fbID. This ID can then be entered to wallpost a URL and/or message. The webinterface makes this easy but from command line you can script (cron) it and easily extend it to send multiple updates at once. A first script (draft) is here


As in the last post you need to have a valid FB APP. See next section where to go to get all preconfigured (= get a valid $access_token to input in your script)

Secondly your App needs to have the following extended permissions

  • publish_stream -> this allows the app to post to walls on your behalf
  • user_likes -> this allows the app to access your liked pages
  • offline_access -> I am still testing this but I believe this prevents the access token to expire in the standard 2 hours (FB permissions page: "Enables your app to perform authorized requests on behalf of the user at any time. By default, most access tokens expire after a short time period to ensure applications only make requests on behalf of the user when the are actively using the application. This permission makes the access token returned by our OAuth endpoint long-lived.")

Thanks to...

The harness for the script I made, comes from - this great post explains how Perl can talk with the Facebook API. Very cool and credits to this article! Of course there is more advanced stuff for accesing the API, but this article is a quick and easy way to get the job done.

Get access token

The only manual preparation is to get the access token in your script, then you are good to go. The only inconvenience is that the token expires if you use the normal permissions. It seems though that the offline_access permission prevents the timeout (and thus saves you from amending the script!)

Script logic

The script follows these steps:

  • you want to get the fbID of a 'friend' or a 'liked' page? If you know this info you can put in 'skip' to break out the first loop;
  • as you can have multiple results per query, you should still enter the fbID after breaking out of the first loop (alternatively make the script to get the fbID from the command line = shift @ARGV ; that was my initial approach but I wanted to have this lookup feature);
  • from this point we know where to post, but what? -> next you are asked to provide an URL and/or message to post to the wall of the fbID
  • Perl calls with method 'post'

Full code example

Again, you can download the code here . It is merely a draft, validation should be build in etc. Another RFE is to do the oauth process automatically.

   #!/usr/bin/perl -w
   #copyright (c) 2011 bob belderbos
   #created: August 2011 
   #special thanks:
   use strict;
   use URI;
   use LWP::Simple;
   use JSON; # imports encode_json, decode_json, to_json and from_json.
   my $access_token = '-----'; 
   # see
   sub build_query {
     my $uri = URI->new(shift);
     return $uri->as_string;
   while(1) { # infinite loop until you get the right fbID to post your link/message to
   	print "Enter 'friends' to find his/her ID, 'likes' to find the page ID, 
  		or enter 'skip' if you know this info: ";
   	chomp(my $cat = );
   	next unless $cat eq 'friends' || $cat eq 'likes' || $cat eq 'skip';
   	last if ($cat =~ /skip/i );
   	print "Enter part of your friend's name or title of the liked page: ";
   	chomp(my $friend = );
   	my $response = get(build_query('' .$cat,
   	  access_token => $access_token
   	my $deserialized = from_json( $response );
   	foreach my $e(@{$deserialized->{data}}){
   		print $e->{id} . ' - ' . $e->{name} . "\n" if $e->{name} =~ /$friend/i;
   	print "Found ID of friend/ page you want to post to? ";
   	chomp(my $answer = );
   	last if ($answer =~ /y(es)?/i );	
   # here you broke the 'lookup' loop so you know which ID to pick.
   # as there might be various candidates, you still have to enter the exact fbID
   print "Enter the ID of the page / friend you want to post to: ";
   chomp(my $fbID = );
   print "Enter the URL you want to send (if only a message, press Enter): ";
   chomp(my $link = );
   print "Enter a message (if you defined an URL, you can leave this blank by pressing Enter: ";
   chomp(my $message = );
   my $response = get(build_query(''.$fbID.'/feed',
     access_token => $access_token,
     message      => $message,
     link         => $link,
     method       => 'post'
   print "Feedback post method: $response\n == END == \n";
   exit 0;


Here you can see the script in action -> 1. command line / 2. result on Facebook :

demo printscreen 1
demo printscreen 2

Also thanks

... to PRAEDO for changing the /me/friends into /me/likes to post to pages instead of friends, and his mention of fbcmd which is a command line interface (CLI) for facebook.

From here on ...

You can subscribe at the top of this page to receive each post in your Inbox. You can also keep up with my blog and discuss Web technology and programming at BobsBlog Facebook group And you can find me on Twitter @bbelderbos

Bob Belderbos

Software Developer, Pythonista, Data Geek, Student of Life. About me