Friday, February 11, 2011

Login to twitter via backend using PERL

This is the same from the last post - Login to foursquare via backend using PERL but this time, we're doing it for twitter. Just follow the 4 simple steps below.

1. Install the libraries into your server.
  • LWP::UserAgent;
  • HTTP::Cookies;
2. Initialize the libraries UserAgent and 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.

2 comments:

  1. Does this no longer work? When I tried it, Twitter threw:

    403 Forbidden: The server understood the request, but is refusing to fulfill it.

    ReplyDelete
  2. Try replacing the one on top from..

    my $lwpua = LWP::UserAgent->new;

    to..

    my $lwpua = LWP::UserAgent->new(
    ssl_opts => { verify_hostname => 0 },
    );

    That should work..

    Thanks,

    ReplyDelete

Leadership 101


  • Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
  • Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
  • Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
  • Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
  • Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
  • Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.