-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauthtest.pl
executable file
·135 lines (110 loc) · 3.26 KB
/
oauthtest.pl
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/perl
# based on http://log.damog.net/2009/05/twitters-oauth-perl/
# http://stereonaut.net/index.php?s=oauth
# use Net::Twitter::OAuth;
# switched back to the Net::Twitter
use strict;
use warnings;
use Net::Twitter;
#use Net::Twitter::Lite::WithAPIv1_1;
use Data::Dumper;
use Storable;
use DateTime;
use Date::Parse;
sub save_tokens {
my %access = @_;
print("saving token: $access{'token'}\n");
print("saving secret: $access{'secret'}\n");
print ("** saved: \n");
## does not compute
warn Dumper(\%access);
# warn Dumper(\$access);
print ("** end dump \n");
store (\%access,'access');
}
sub restore_tokens {
eval {
my %access = %{ retrieve('access') };
print ("** restored: \n");
print("restored token: $access{'token'}\n");
print("restored secret: $access{'secret'}\n");
print ("** dump restored access: \n");
print ("*** begin dump \n");
warn Dumper(\%access);
print ("*** end dump \n");
return(\%access);
} or do {
print("!!! Could not restore token\n");
my %hash = ();
return (\%hash);
}
}
#my $client = Net::Twitter::Lite::WithAPIv1_1->new(
my $client = Net::Twitter->new(
traits => [qw/API::RESTv1_1 InflateObjects/],
consumer_key => "IkU8CVvABj0ZeOQrAQDrvg",
consumer_secret => "kDB5lMR0VoQEbLIrbuvLD72j7XrozVgEyHP0q4csc",
);
# we should not restore if the file isn't there
my ($access) = restore_tokens();
#my $access = {};
print("--- access ---\n");
warn Dumper($access);
print("----------\n");
if ($access->{'token'}) {
print("restored\n");
print("access_token: $access->{'token'}\n");
print("access_token_secret: $access->{'secret'}");
$client->access_token($access->{'token'});
$client->access_token_secret($access->{'secret'});
}
# reading pin
my $data_file="pin";
my $newpin = "";
if (open(DAT, $data_file)){
my @raw_data=<DAT>;
close(DAT);
foreach my $pin (@raw_data)
{
chomp($pin);
print "pin: $pin\n";
$newpin = $pin;
last;
}
# my $newpin = $pin;
print "newpin: $newpin\n";
}
if (not ( $client->authorized )) {
# The client is not yet authorized: Do it now
print "Authorize this app at ", $client->get_authorization_url, ", enter the pin and hit RET\n";
$newpin = <STDIN>; # wait for input
chomp($newpin);
my %access;
($access{'token'}, $access{'secret'}) =
$client->request_access_token(verifier => $newpin);
save_tokens(%access); # if necessary
print("access_token: $access{'token'}\n");
print("access_token_secret: $access{'secret'}\n");
}
# my $res = $client->update({ status => 'me ownz oauth!!1' });
eval {
# my $statuses = $client->friends_timeline({ since_id => $high_water, count => 100 });
my $statuses = $client->user_timeline({ count => 1 });
my $timeout_time = DateTime::Duration->new( hours => 48);
for my $status ( @$statuses ) {
my $age = DateTime->now - $status->created_at;
if (DateTime::Duration->compare( $timeout_time, $age) == -1) {
print('msg to old: timeout time');
}
print "$status->{created_at} <$status->{user}{screen_name}> $status->{text} $age\n";
}
};
if ( my $err = $@ ) {
die $@ unless $err->isa('Net::Twitter::Error');
warn "HTTP Response Code: ", $err->code, "\n",
"HTTP Message......: ", $err->message, "\n",
"Twitter error.....: ", $err->error, "\n";
}
# warn Dumper(\$client);
$client->end_session;
warn Dumper(\$client);