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