Skip to content

Instantly share code, notes, and snippets.

@ceekz
Created June 20, 2014 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceekz/aac7cad058291cb761cb to your computer and use it in GitHub Desktop.
Save ceekz/aac7cad058291cb761cb to your computer and use it in GitHub Desktop.
Create an access token from a consumer key in Twitter.
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use Net::Twitter;
my $cgi = new CGI;
if ($cgi->param('oauth_verifier')) {
my $nt = Net::Twitter->new(
traits => [qw/API::RESTv1_1/],
consumer_key => $cgi->cookie('consumer_key') || $cgi->param('consumer_key'),
consumer_secret => $cgi->cookie('consumer_secret') || $cgi->param('consumer_secret'),
);
$nt->request_token($cgi->cookie('token'));
$nt->request_token_secret($cgi->cookie('token_secret'));
my($access_token, $access_token_secret, $user_id, $screen_name) = $nt->request_access_token(verifier => $cgi->param('oauth_verifier'));
print $cgi->header(-type=>'text/plain', -charset=>'utf-8');
printf "consumer_key => '%s',\n", $cgi->cookie('consumer_key') || $cgi->param('consumer_key');
printf "consumer_secret => '%s',\n", $cgi->cookie('consumer_secret') || $cgi->param('consumer_secret');
printf "access_token => '%s',\n", $access_token;
printf "access_token_secret => '%s',\n", $access_token_secret;
print "\n";
} else {
my $consumer_key = $cgi->cookie('consumer_key') || '';
my $consumer_secret = $cgi->cookie('consumer_secret') || '';
print $cgi->header(-type=>'text/html', -charset=>'utf-8');
print <<"__HTML__";
<form action="?" method="post">
consumer_key <input type="text" name="consumer_key" value="$consumer_key">
consumer_secret <input type="text" name="consumer_secret" value="$consumer_secret">
PIN <input type="text" name="oauth_verifier">
<input type="submit" value="send">
</form>
__HTML__
}
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use Net::Twitter;
my $cgi = new CGI;
if ($cgi->param('consumer_key') && $cgi->param('consumer_secret')) {
my $nt = Net::Twitter->new(
traits => [qw/API::RESTv1_1/],
consumer_key => $cgi->param('consumer_key'),
consumer_secret => $cgi->param('consumer_secret'),
);
my $url = $nt->get_authorization_url();
print 'Set-Cookie: ' . $cgi->cookie(-name => 'consumer_key', -value => $cgi->param('consumer_key')) . "\n";
print 'Set-Cookie: ' . $cgi->cookie(-name => 'consumer_secret', -value => $cgi->param('consumer_secret')) . "\n";
print 'Set-Cookie: ' . $cgi->cookie(-name => 'token', -value => $nt->request_token) . "\n";
print 'Set-Cookie: ' . $cgi->cookie(-name => 'token_secret', -value => $nt->request_token_secret) . "\n";
print $cgi->redirect(-uri => $url);
} else {
print $cgi->header(-type=>'text/html', -charset=>'utf-8');
print <<"__HTML__";
<form action="?" method="post">
consumer_key <input type="text" name="consumer_key">
consumer_secret <input type="text" name="consumer_secret">
<input type="submit" value="send">
</form>
<ul>
<li><a href="pin.cgi">pin.cgi</a></li>
<li><a href="https://gist.github.com/rhenium/3878505">https://gist.github.com/rhenium/3878505</a> (Twitter for Google TV => NG!!)</li>
</ul>
__HTML__
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment