If you still have no idea on how the twitter works, just follow the steps below and you will be able to send tweets in minutes.
1. Same on how we do it on facebook, we need to install the following libraries on our server.
- LWP::UserAgent
- HTTP::Cookies
2. Once installed, we are now ready to do the coding part. we have to require and initialize the libraries and variables that we will be using.
require LWP::UserAgent;
use strict;
use warnings;
use HTTP::Cookies;
my $lwpua = LWP::UserAgent->new;
my $user_agent = "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' => 'http://mobile.twitter.com/','User-Agent' => $user_agent);
my $cookie_file = "cookies.dat";
my $cookie_jar = HTTP::Cookies->new(
file => $cookie_file,
autosave => 1,
ignore_discard => 1);
$lwpua->cookie_jar($cookie_jar);
3. Input your twitter account username, password, and your tweet you wish to be posted.
my $strUser = "<your twitter username>";
my $strPass = "<your twitter password>";
my $strTweet = "<your tweet>";
4. Get the authenticity_token using REGEX from the twitter session url http://mobile.twitter.com/session/new. token will be use as parameter in submitting your credentials to the wap site. i also pasted the form element of the login page below as reference.
=begin
<form action="https://mobile.twitter.com/session" method="post">
<input name="authenticity_token" type="hidden" value="22a9872b3a17abd635a4" />
<input autocapitalize="off" autocorrect="off" id="username" name="username" type="text" />
<input id="password" name="password" type="password" />
<input class="signup" type="submit" value="Sign in" />
</form>
=cut
$form_data =~ s/\n//g;
$form_data =~ /input name="authenticity_token" type="hidden" value="(.*?)"/ig;
my $auth_token = $1;
5. Use the token to login by posting it to the form action url above: https://mobile.twitter.com/session and save the cookie for the nest session.
# logged in to twitter
$response = $lwpua->post('https://mobile.twitter.com/session',
['username' => $strUser,
'password' => $strPass,
'authenticity_token' => $auth_token], @header);
$cookie_jar->extract_cookies( $response );
$cookie_jar->save;
$response = $lwpua->get('http://mobile.twitter.com', @header);
$form_data = $response->content;
6. Upon submission of credentials, you should be able to log-in successfully. to check it, you should be able to see the "What's happenning?" and the textarea in which you will input your tweet. again, get the authenticity token using REGEX below for the next session.
=begin
<form action="http://mobile.twitter.com/" class="new_tweet" id="new_tweet" method="post">
<input name="authenticity_token" type="hidden" value="22a9872b3a17abd635a4" />
<div class="tweetbox-head">What's happening?</div>
<textarea class="tweet_input" cols="44" id="tweet_text" name="tweet[text]" rows="2"></textarea>
<input class="tweet-btns" type="submit" value="Tweet" />
</form>
=cut
$form_data =~ s/\n//g;
$form_data =~ /input name="authenticity_token" type="hidden" value="(.*?)"/ig;
$auth_token = $1;
7. And now, you are ready to post your tweet. by passing tweet[text] parameter together with the authenticity_token.
# posting tweet on twitter
@header = ( 'Referer' => 'http://mobile.twitter.com/', 'User-Agent' => $user_agent );
$response = $lwpua->post('http://mobile.twitter.com/',
['tweet[text]' => $strTweet,
'authenticity_token' => $auth_token], @header);
$form_data = $response->content;
unlink($cookie_file);
8. To get the complete code, please see below. hope you were able to follow. happy coding!!
#!/usr/bin/perl
require LWP::UserAgent;
use strict;
use warnings;
use HTTP::Cookies;
my $lwpua = LWP::UserAgent->new;
my $user_agent = "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' => 'http://mobile.twitter.com/','User-Agent' => $user_agent);
my $cookie_file = "cookies.dat";
my $cookie_jar = HTTP::Cookies->new(
file => $cookie_file,
autosave => 1,
ignore_discard => 1);
$lwpua->cookie_jar($cookie_jar);
my $strUser = "<your twitter username>";
my $strPass = "<your twitter password>";
my $strTweet = "<your tweet>";
# get authenticity token
my $response = $lwpua->get('http://mobile.twitter.com/session/new', @header);
my $form_data = $response->content;
$form_data =~ s/\n//g;
$form_data =~ /input name="authenticity_token" type="hidden" value="(.*?)"/ig;
my $auth_token = $1;
# logged in to twitter
$response = $lwpua->post('https://mobile.twitter.com/session',
['username' => $strUser,
'password' => $strPass,
'authenticity_token' => $auth_token], @header);
$cookie_jar->extract_cookies( $response );
$cookie_jar->save;
$response = $lwpua->get('http://mobile.twitter.com', @header);
$form_data = $response->content;
$form_data =~ s/\n//g;
$form_data =~ /input name="authenticity_token" type="hidden" value="(.*?)"/ig;
$auth_token = $1;
# posting tweet on twitter
@header = ( 'Referer' => 'http://mobile.twitter.com/', 'User-Agent' => $user_agent );
$response = $lwpua->post('http://mobile.twitter.com/',
['tweet[text]' => $strTweet,
'authenticity_token' => $auth_token], @header);
$form_data = $response->content;
unlink($cookie_file);
1;
No comments:
Post a Comment