Developing Twitter Apps with Perl

Most of the Twitter-related blogs and sample code on the web are obsolete, so I wrote this overview for 2015.

The most important things to know are:

  1. Twitter API 1.1 is required using OAuth and SSL
  2. your must register with Twitter for two sets of tokens:
    1. consumer credentials (API key).
    2. Twitter-approved OAuth tokens. To register for these, you need a Twitter account with an associated mobile phone number. Since Twitter has a unique constraint on mobile numbers, that means you have to register a new phone number or move an old phone number from an existing account. To move your phone number, just SMS “Start” to 40404 and answer the messages sent back. You need to know the password of the new Twitter account, but not the old account.
  3. The Perl CPAN module I use is Net::Twitter
  4. to do multi-tenant tweeting, use the same consumer key as above but request the OAuth tokens from each client.

The Net:Twitter API calls I use are:

  1. update (tweet)
    use Net::Twitter::Lite::WithAPIv1_1;
       my $nt = Net::Twitter::Lite::WithAPIv1_1->new(
           consumer_key        => 'abc',
           consumer_secret     => 'def',
           access_token        => 'ghi',
           access_token_secret => 'jkl',
           ssl => 1,
       my $status = 'Hello, world!';
       my $o $nt->update($status);
       my $status_id = 0;
       if (defined $o) {
          $status_id = $o->{'id'};
          print "status id=$status_id\n";
  2. destroy_status (delete tweet)
       my $o = $nt->destroy_status($status_id);

To delete a tweet, you must save the status ID of the tweet. (Likely that means inserting the status ID in a database.) Also, only the account who tweeted can delete the tweet.

This entry was posted in API Programming, Tech. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.