Post to Twitter from Perl using a single access token (API v1.1)

06 May 2010

You want to post status updates to your own Twitter account from a Perl script. Because of the new Twitter API you need to authenticate using OAuth, but you don’t want (nor need, nor care) to do the “OAuth token request” or whatever dance. It’s my own account, I just want to post!

  1. Go to dev.twitter.com and register a new application. From this you will obtain a consumer key and a consumer secret.

  2. Once your app is registered, click on “My access token” on the details page of your application. From this you will obtain an access token and an access token secret.

  3. Go and code in Perl using Net::Twitter::Lite::WithAPIv1_1.

Example code:

use Net::Twitter::Lite::WithAPIv1_1;
use Scalar::Util 'blessed';

my $nt = Net::Twitter::Lite::WithAPIv1_1->new(
  consumer_key        => 'your consumer key',
  consumer_secret     => 'your consumer secret',
  access_token        => 'your access token',
  access_token_secret => 'your access token secret'
);

my $result = eval { $nt->update('Hello, World! http://nokyotsu.com/qscripts/2010/05/post-to-twitter-from-perl-using-single.html') };

if ( my $err = $@ ) {
  die $@ unless blessed $err && $err->isa('Net::Twitter::Lite::Error');

  warn "HTTP Response Code: ", $err->code, "\n",
       "HTTP Message......: ", $err->message, "\n",
       "Twitter error.....: ", $err->error, "\n";
}
  • Updated on 14th June 2013 with Twitter API v1.1 support.