In posting a tweet, we will use the resource URL - http://api.twitter.com/1/statuses/update.json and the POST method.
Just follow the steps below to post tweet on users behalf.
1. Install the following libraries.
- LWP::UserAgent
- HTTP::Cookies
- Digest::MD5
- Digest::SHA
- POSIX
2. Initialize the libraries.
require LWP::UserAgent;
use HTTP::Cookies;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use Digest::SHA qw(sha1 sha1_hex sha1_base64 hmac_sha1 hmac_sha1_hex hmac_sha1_base64);
use POSIX qw(strftime);
my $lwpua = LWP::UserAgent->new;
3. Setup the UserAgent and HTTP header with the name "Authorization" equal to "OAuth".
my $uagent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
my @header = ('Referer' => 'https://api.twitter.com/',
'User-Agent' => $uagent,
'Authorization' => 'OAuth');
4. Build the signature base and signing key. Please take note that some parameters should be URL Encoded, see the post for the PERL function that you can use - URLENCODE sub function in PERL.
You need the consumer key and secret which was generated by Twitter upon registration.
You also need the Oauth Token and Secret which you get when exchanging the requested token for an access token.
To generate NONCE, we use Digest::MD5::md5_base64() passing the localtime(). We do it twice to make it much UNIQUE.
Timestamp was generated by using POSIX::strftime() with date format UNIXTIME as "%s".
Signing key was generated by combining the 2 parameters: Consumer Secret and OAuth Token Secret delimited by ampersand "&"
my $strToken = "<your oauth access token>";
my $strSecret = "<your oauth access token secret>";
my $strStatus = "<your tweet>";
my $post_url = "http://api.twitter.com/1/statuses/update.json";
my $nonce = md5_base64(localtime()).md5_base64(localtime());
my $timestamp = strftime('%s', localtime);
my $method = "HMAC-SHA1";
my $version = "1.0";
my $consumer_key = "<your consumer key generated upon registration>";
my $consumer_secret = "<your consumer secret generated upon registration>";
my $post_urlenc = &urlencode($post_url);
my $status_enc = &urlencode($strStatus);
my $oauth_data = "oauth_consumer_key=$consumer_key&oauth_nonce=$nonce&oauth_signature_method=$method&oauth_timestamp=$timestamp&oauth_token=$strToken&oauth_version=$version&status=$status_enc";
my $oauth_dataenc = &urlencode($oauth_data);
my $sign_base = "POST&".$post_urlenc."&".$oauth_dataenc;
my $sign_key = $consumer_secret."&".$strSecret;
5. Generate the OAuth Signature using HMAC-SHA1 method.
my $oauth_sign = hmac_sha1_base64($sign_base, $sign_key)."=";
6. Post an OAuth request with all the parameters you passed on step #4.
my $response = $lwpua->post($post_url,
['oauth_token' => $strToken,
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => $nonce,
'oauth_signature_method' => $method,
'oauth_signature' => $oauth_sign,
'oauth_timestamp' => $timestamp,
'status' => $strStatus,
'oauth_version' => $version], @header);
print $response->content;
7. To know if successful, you can print the response and it should be look like this.
{
"geo":null,
"created_at":"Mon Apr 26 23:45:50 +0000 2010",
"in_reply_to_user_id":null,
"truncated":false,
"place":null,
"source":"<a href=\"http://dev.twitter.com\" rel=\"nofollow\">OAuth Dancer</a>",
"favorited":false,
"in_reply_to_status_id":null,
"contributors":null,
"user":
{
"contributors_enabled":false,
"created_at":"Wed Mar 07 22:23:19 +0000 2007",
"description":"Reality Technician,
Developer Advocate at Twitter,
Invader from the Space.",
"geo_enabled":true,
"notifications":false,
"profile_text_color":"000000",
"following":false,
"time_zone":"Pacific Time (US & Canada)",
"verified":false,
"profile_link_color":"731673",
"url":"http://bit.ly/5w7P88",
"profile_background_image_url":"http://a3.twimg.com/profile_background_images/19651315/fiberoptics.jpg",
"profile_sidebar_fill_color":"007ffe",
"location":"San Francisco,
CA",
"followers_count":1220,
"profile_image_url":"http://a3.twimg.com/profile_images/836683953/zod_normal.jpg",
"profile_background_tile":true,
"profile_sidebar_border_color":"bb0e79",
"protected":false,
"friends_count":1275,
"screen_name":"episod",
"name":"Taylor Singletary",
"statuses_count":5733,
"id":819797,
"lang":"en",
"utc_offset":-28800,
"favourites_count":89,
"profile_background_color":"000000"
},
"in_reply_to_screen_name":null,
"coordinates":null,
"id":12912397434,
"text":"setting up my twitter \u79c1\u306e\u3055\u3048\u305a\u308a\u3092\u8a2d\u5b9a\u3059\u308b"
}
Please see the complete code below.
#!/usr/bin/perl
require LWP::UserAgent;
use strict;
use warnings;
use HTTP::Cookies;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use Digest::SHA qw(sha1 sha1_hex sha1_base64 hmac_sha1 hmac_sha1_hex hmac_sha1_base64);
use POSIX qw(strftime);
my $lwpua = LWP::UserAgent->new;
my $uagent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
my @header = ('Referer' => 'https://api.twitter.com/',
'User-Agent' => $uagent,
'Authorization' => 'OAuth');
# build signature base
my $strToken = "<your oauth access token>";
my $strSecret = "<your oauth access token secret>";
my $strStatus = "<your tweet>";
my $post_url = "http://api.twitter.com/1/statuses/update.json";
my $nonce = md5_base64(localtime()).md5_base64(localtime());
my $timestamp = strftime('%s', localtime);
my $method = "HMAC-SHA1";
my $version = "1.0";
my $consumer_key = "<your consumer key generated upon registration>";
my $consumer_secret = "<your consumer secret generated upon registration>";
my $post_urlenc = &urlencode($post_url);
my $status_enc = &urlencode($strStatus);
my $oauth_data = "oauth_consumer_key=$consumer_key&oauth_nonce=$nonce&oauth_signature_method=$method&oauth_timestamp=$timestamp&oauth_token=$strToken&oauth_version=$version&status=$status_enc";
my $oauth_dataenc = &urlencode($oauth_data);
my $sign_base = "POST&".$post_urlenc."&".$oauth_dataenc;
my $sign_key = $consumer_secret."&".$strSecret;
# oauth signature
my $oauth_sign = hmac_sha1_base64($sign_base, $sign_key)."=";
# post oauth request
my $response = $lwpua->post($post_url,
['oauth_token' => $strToken,
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => $nonce,
'oauth_signature_method' => $method,
'oauth_signature' => $oauth_sign,
'oauth_timestamp' => $timestamp,
'status' => $strStatus,
'oauth_version' => $version], @header);
print $response->content;
1;
No comments:
Post a Comment