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:
- Twitter API 1.1 is required using OAuth and SSL
- your must register with Twitter for two sets of tokens:
- consumer credentials (API key).
- 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.
- The Perl CPAN module I use is Net::Twitter
- 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:
- 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"; }
- 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.