You may have already noticed that, while I surely do not refuse graphical interfaces and cool, modern Linux desktop environments, I am always looking for ways to perform every computer-related task from the command line. Sometimes it is because I actually need to automate something. In other cases it is just for the fun of it, to discover if it’s possible. This is the case of the trick I am going to explain this week: how to post to one’s Facebook wall, or read it, from a script.
Of course, you may be wondering why a Free Software / Digital Rights supporter may want to do something like this. In my opinion, there are both general reasons and real-world situations why knowing such a trick would be useful. Facebook may be evil, the greatest attack to privacy ever invented, and so on, but that’s not the point (not here, anyway). Whatever one may think of Facebook, having a presence there is unavoidable these days, if you need to reach the many people who only go online to use Facebook. Wouldn’t it be very convenient, for example, to announce automatically on Facebook new releases of some Free Software application? Here’s how to do it.
Dave Tompkins has written a nice, little command line Facebook interface called fbcmd, that you may use as basis for your own application. Here I’ll only show how to set it up for the simplest, but also most useful task, that is, posting a short status update to your wall.
To begin, install php on your computer if it isn’t there already, then download the fb_cmd_update.php script. The first time you’ll execute that script with the PHP interpreter:
[marco@facebook_cmd]# php fbcmd_update.php
It will greet you by listing the default configuration settings and explaining how to change them, if necessary. Once you’ve done that, you’ll have to type:
[marco@facebook_cmd]# php fbcmd_update.php install
With this operation, you will install the actual PHP script that talks to Facebook, called fbmcd.php, and its shell wrapper, that is the command (fbcmd) that you will actually run. However, these files will still be unable to talk with Facebook. You’ll have to explicitly log in to Facebook and authorize fb_cmd to post to your wall.
Authorizing the script to access your account
If you run fbcmd before it has loaded the right authorization code from Facebook, it will ask you to run a couple of extra commands:
Step 1: Allow basic (initial) access to your account via [a Facebook URL].
To launch this page, execute: fbcmd go access
Step 2: Generate an offline authorization code at [another Facebook URL].
To launch this page, execute: fbcmd go auth
In practice, it is not necessary to do so. You may go to the fb_cmd installation page and click on the link given in the “Basic Access” paragraph. This action will make Facebook ask your permission to give fb_cmd “basic access” to your account (see Figure A).
The next step is to retrieve from Facebook the access credentials that fb_cmd can use to work on your account all by itself. This is a simple two-step procedure: first, click on the link given in the “Authorization Code” section of the installation page. Next, pass to the script the authorization code that Facebook returned in this way:
fbcmd auth AUTHCODE
Please note that, if you plan to have several Facebook users using the script, you will have to repeat this procedure for each of them! By default, fbcmd saves all the user-specific configuration and session data in the folder $HOME/.fbcmd. You can tell the software to use another folder by writing its path in the environment variable FBCMD.
How to authorize status updates
The steps described so far don’t accomplish much, in practice. If you want fbcmd to do something useful, you need to perform extra authorization steps. This is something for which I have found the documentation on the website and the help message given by the script a bit confusing, or maybe not really up to date.
For example, in spite of what the script itself told me to do, eventually I found that the easiest way to set up automatic status updates, was to just go to the fb_cmd troubleshooting page and click on the link provided to “grant FBCMD the PUBLISH_STREAM permissions”. By doing so, you will open a Facebook form as in Figure B, and all you’ll have to do will be to click on “Allow Publishing.”
Post to your Facebook wall with fbcmd
If there were no errors in the steps above, at this point fbcmd will be ready to post.
As you can see in Figure C, I typed:
fbcmd post "Here is my first Facebook ^Catus update from the command line"
and a few seconds later the status update appeared in my Facebook wall, spelling error included. Of course, the same thing could (and should, since this is one of the main reasons to use fbcmd) be done inside a shell script!
Last but not least: Back up your Facebook data!
Fbcmd can do much, much more than posting status updates: Thanks, Dave! Right now, I don’t plan to ever use any of those other functions, with one exception, which I strongly advise all of you to follow: back up on your computer all your status updates and posts from your friends. Fbcmd provides two commands for this very purpose, that is, making complete or incremental backups of your Facebook wall. Set up a cron job to run them periodically, as I am going to do as soon as I finish writing this post, and you’ll always have your own copy of your Facebook activity.