Skip to content

Simple Tweeting from Perl using Net::Twitter

March 6, 2011

The following is a simple Perl script that Tweets using the CPAN Net::Twitter API to an OAuth’ed Twitter account:

my $tweet = 'I\'m tweeting from an OAuth PERL app everybody!';

my $twitterconsumer = $yourconsumerkey;
my $twitterconsumersecret = $yourconsumersecret;
my $twitteraccesstoken = $youraccesstoken;
my $twitteraccesstokensecret = $youraccesstokensecret;

my $nt = Net::Twitter->new(
	traits          => ['API::REST', 'OAuth'],
	consumer_key    => $twitterconsumer,
	consumer_secret => $twitterconsumersecret,
);

if ($twitteraccesstoken && $twitteraccesstokensecret) {
	$nt->access_token($twitteraccesstoken);
	$nt->access_token_secret($twitteraccesstokensecret);
}

unless ( $nt->authorized ) {
	print "Authorize this app at ", $nt->get_authorization_url, " and enter the PIN#\n";
	
	my $pin = ; # wait for input
	chomp $pin;
	
	my($access_token, $access_token_secret, $user_id, $screen_name) = $nt->request_access_token(verifier => $pin);
	print 'Access token: '.$access_token."\r\n".'Access Token Secret: '.$access_token_secret."\r\n";

	exit();

}

$nt->update({ status => $tweet });

To get it working you just need to do the following:

  1. Register your app at dev.twitter.com/apps to get your Consumer Key and Consumer Secret. Populate them in the script.
  2. Make sure you have Net::Twitter installed – type cpan at the command prompt and then install Net::Twitter to install it or check for updates.
  3. Run the Perl script once – it will give you a URL to go and ask you for a PIN. Go to the URL and get your PIN, and then enter the PIN and the script will return the Access Token and Access Token Secret. Put those in the script variables.
  4. The script will now Tweet whatever is in the $tweet variable
Advertisements

From → Programming

7 Comments
  1. Lee Rothstein permalink

    I believe the he script has been modified by WordPress and doesn’t work.

    For example, isn’t the correct line for the above?:

    “my $pin = ; # wait for input”

    Wasn’t the ” deleted?

    Also, isn’t the following line missing at the start:

    use Net::Twitter;

    And, aren’t the following variables undefined in the script?:

    $user_id
    $screen_name

    Also, don’t you need a password?

    • Lee Rothstein permalink

      Let’s try that again:

      I believe the script has been modified by WordPress and doesn’t work.
      
      For example, isn’t the correct line for the above?:
      
      “my $pin = ; # wait for input”
      
      Wasn’t the “” deleted?
      
      Also, isn’t the following line missing at the start:
      
      use Net::Twitter;
      
      And, aren’t the following variables undefined in the script?:
      
      $user_id
      $screen_name
      
      Also, don’t you need a password?
      
      
  2. Lee Rothstein permalink

    WordPress sucks as a vehicle for source distribution and documentation!

  3. Lee Rothstein permalink

    Third try is a charm!?:

    I believe the script has been modified by WordPress and doesn’t work.
    
    For example, isn’t the correct line for the above?:
    
    “my $pin = <>; # wait for input”
    
    Wasn’t the "<>" deleted?
    
    Also, isn’t the following line missing at the start:
    
    use Net::Twitter;
    
    And, aren’t the following variables undefined in the script?:
    
    $user_id
    $screen_name
    
    Also, don’t you need a password?
    

    Also, even with all these corrections (above) I seem to be in some limbo state, and can;t recover. Help!

  4. Thanks, this was very helpful for getting started.

  5. eduardo permalink

    hello! I’ve executed your steps (register the app and install Net::Twitter), but the script doesn’t print any URL. Do you have any idea what to do?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: