1. Install the libraries into your server.
- LWP::UserAgent;
- 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://m.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. Post your username and password to https://mobile.twitter.com/session and save the cookies.
my $strUser = '<your twitter username>';
my $strPass = '<your twitter password>';
# log-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;
4. You can check if successful or not by accessing https://mobile.twitter.com via GET and print the content.
$response = $lwpua->get('https://mobile.twitter.com', @header);
$form_data = $response->content;
print "$form_data\n\ndone!";
That's it! Please see the complete code below.
#!/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://m.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);
# getting authenticity token
my $response = $lwpua->get('https://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;
my $strUser = '<your twitter username>';
my $strPass = '<your twitter password>';
# log-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('https://mobile.twitter.com', @header);
$form_data = $response->content;
print "$form_data\n\ndone!";
1;
Hope you like it!! Thank you for reading.
Does this no longer work? When I tried it, Twitter threw:
ReplyDelete403 Forbidden: The server understood the request, but is refusing to fulfill it.
Try replacing the one on top from..
ReplyDeletemy $lwpua = LWP::UserAgent->new;
to..
my $lwpua = LWP::UserAgent->new(
ssl_opts => { verify_hostname => 0 },
);
That should work..
Thanks,