Monday, February 14, 2011

How to authorize OAuth TOKEN from Twitter in PERL

I assume you read my previous post - How to request OAuth TOKEN from Twitter in PERL before reading this post. You need the OAuth Token and Secret to proceed to this process.

On this post, we will login to twitter via back-end, please see post - Login to Twitter via backend using PERL on how to do it. Once the user had been logged in, we will authorize our application to post tweet and access users information in users behalf.

This post will outputted an OAuth Token and Verifier which you will use to Access Token "final oauth process" to get the OAuth Token and OAuth Secret.

Follow the simple steps below to authorize your application.

1. Install the following libraries.
  • LWP::UserAgent
  • HTTP::Cookies

2. Initialize the libraries.

require LWP::UserAgent;
use HTTP::Cookies;
my $lwpua = LWP::UserAgent->new;

3. Setup UserAgent, HTTP Header, and Cookies.

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);

my $cookie_file = "/home/wapps/tweext/cookies/twitter/cookies.$intMsisdn.dat";
my $cookie_jar = HTTP::Cookies->new(
                file => $cookie_file,
                autosave => 1,
                ignore_discard => 1);

my $lwpua->cookie_jar($cookie_jar);

4. Post the OAuth Token with Username and Password to authorize URL - https://api.twitter.com/oauth/authorize

my $strToken = "<your oauth token>";
my $strUser = "<your twitter username>";
my $strPass = "<your twitter password>";

my $response = $lwpua->post("https://api.twitter.com/oauth/authorize",
                          ['oauth_token' => $strToken,
                           'session[username_or_email]' => $strUser,
                           'session[password]' => $strPass], @header);
my $cookie_jar->extract_cookies( $response );
my $cookie_jar->save;

$form_data = $response->content;

5. Get the OAuth Token and Oauth Verifier from the output returned by step #4.

$form_data =~ s/\n//g;
$form_data =~ /meta http-equiv="refresh" content="0;url=(.*?)\?oauth_token=(.*?)&oauth_verifier=(.*?)"/ig;

my $oauth_token = $2;
my $oauth_verif = $3;

unlink($cookie_file);



1;


Please see the complete code below. Thank you for reading this post.

#!/usr/bin/perl

require LWP::UserAgent;

use strict;
use warnings;

use HTTP::Cookies;

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);

my $cookie_file = "/home/wapps/tweext/cookies/twitter/cookies.$intMsisdn.dat";
my $cookie_jar = HTTP::Cookies->new(
                file => $cookie_file,
                autosave => 1,
                ignore_discard => 1);

my $lwpua->cookie_jar($cookie_jar);

my $strToken = "<your oauth token>";
my $strUser = "<your twitter username>";
my $strPass = "<your twitter password>";

my $response = $lwpua->post("https://api.twitter.com/oauth/authorize",
                          ['oauth_token' => $strToken,
                           'session[username_or_email]' => $strUser,
                           'session[password]' => $strPass], @header);
my $cookie_jar->extract_cookies( $response );
my $cookie_jar->save;

$form_data = $response->content;

$form_data =~ s/\n//g;
$form_data =~ /meta http-equiv="refresh" content="0;url=(.*?)\?oauth_token=(.*?)&oauth_verifier=(.*?)"/ig;

my $oauth_token = $2;
my $oauth_verif = $3;

unlink($cookie_file);



1;

No comments:

Post a Comment

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.