Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subclass Net::Twitter so it does the authentication.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |