-
Notifications
You must be signed in to change notification settings - Fork 2
/
oauth
executable file
·58 lines (47 loc) · 1.58 KB
/
oauth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env perl
use Net::Twitter;
our $consumer_key = q(key);
our $consumer_secret = q(secret);
my $nt = Net::Twitter->new(
traits => ['API::RESTv1_1', 'OAuth'],
consumer_key => $consumer_key,
consumer_secret => $consumer_secret,
ssl => 1,
);
my ($access_token, $access_token_secret) = restore_tokens();
if ($access_token && $access_token_secret) {
$nt->access_token($access_token);
$nt->access_token_secret($access_token_secret);
}
unless ($nt->authorized) {
print "Authorize this app at ", $nt->get_authorization_url, " and enter the PIN.\n";
my $pin = <STDIN>;
chomp $pin;
my ($access_token, $access_token_secret, $user_id, $screen_name) = $nt->request_access_token(verifier => $pin);
save_tokens($access_token, $access_token_secret);
}
print "Done.\n";
exit(0);
sub restore_tokens {
open my $fh, '<', ".oauth" or die qq(failed to create file; $!);
my ($access_token, $access_token_secret) = map {
chomp;
s/^[^:]+:// if defined and length;
$_
} <$fh>;
close $fh;
return ($access_token, $access_token_secret);
}
sub save_tokens {
die qq(Not enough arguments to save_tokens) unless @_ > 1;
my ($access_token, $access_token_secret) = @_;
die qq(Access token is required) unless $access_token;
die qq(Access token secret is required) unless $access_token_secret;
open my $fh, '>', ".oauth" or die qq(failed to create file; $!);
my $ofh = select $fh;
print "token:$access_token\n";
print "secret:$access_token_secret\n";
select $ofh;
close $fh;
}
__END__