Skip to content

Commit

Permalink
Subclass Net::Twitter so it does the authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Aug 17, 2014
1 parent 48b42ec commit f6afc5b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/TwittElection/Twitter.pm
@@ -0,0 +1,45 @@
package TwittElection::Twitter;

use Moose;
extends 'Net::Twitter';

sub authorise {
my $self = shift;

my($access_token, $access_token_secret) = restore_tokens();

if ($access_token && $access_token_secret) {
$self->access_token($access_token);
$self->access_token_secret($access_token_secret);
}

unless ( $self->authorized ) {
# The client is not yet authorized: Do it now
print "Authorize this app at ", $self->get_authorization_url,
" and enter the PIN#\n";

my $pin = <STDIN>; # wait for input
chomp $pin;

my($access_token, $access_token_secret, $user_id, $screen_name) =
$self->request_access_token(verifier => $pin);
save_tokens($access_token, $access_token_secret); # if necessary
}
}

sub save_tokens {
my ($access_token, $access_token_secret) = @_;

open my $tw_fh, '>', '.te_tokens' or die $!;
print $tw_fh "$access_token $access_token_secret";
}

sub restore_tokens {
open my $tw_fh, '<', '.te_tokens' or die $!;

my $tokens = <$tw_fh>;

return split / /, $tokens;
}

1;

0 comments on commit f6afc5b

Please sign in to comment.