/
oauth_webapp.pl
133 lines (101 loc) · 3.56 KB
/
oauth_webapp.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
#!/usr/bin/perl
#
# Net::Twitter - OAuth webapp example
#
package MyWebApp;
use warnings;
use strict;
use base qw/HTTP::Server::Simple::CGI/;
use Net::Twitter;
use Data::Dumper;
# You can replace the consumer tokens with your own;
# these tokens are for the Net::Twitter example app.
my %consumer_tokens = (
consumer_key => 'v8t3JILkStylbgnxGLOQ',
consumer_secret => '5r31rSMc0NPtBpHcK8MvnCLg2oAyFLx5eGOMkXM',
);
my $server_port = 8080;
sub twitter { shift->{twitter} ||= Net::Twitter->new(traits => [qw/API::REST OAuth/], %consumer_tokens) }
my %dispatch = (
'/oauth_callback' => \&oauth_callback,
'/' => \&my_last_tweet,
);
# all request start here
sub handle_request {
my ($self, $q) = @_;
my $request = $q->path_info;
warn "Handling request for $request\n";
my $handler = $dispatch{$request} || \¬_found;
$self->$handler($q);
}
# Display the authenicated user's last tweet in all its naked glory
sub my_last_tweet {
my ($self, $q) = @_;
# if the user is authorized, we'll get access tokens from a cookie
my %sess = $q->cookie('sess');
unless ( %sess ) {
warn "User has no access_tokens\n";
return $self->authorize($q);
}
warn <<"";
Using access tokens:
access_token => $sess{access_token}
access_token_secret => $sess{access_token_secret}
my $nt = $self->twitter;
# pass the access tokens to Net::Twitter
$nt->access_token($sess{access_token});
$nt->access_token_secret($sess{access_token_secret});
# attempt to get the user's last tweet
my $status = eval { $nt->user_timeline({ count => 1 }) };
if ( $@ ) {
warn "$@\n";
# if we got a 401 response, our access tokens were invalid; get new ones
return $self->authorize($q) if $@ =~ /\b401\b/;
# something bad happened; show the user the error
$status = $@;
}
print $q->header(-nph => 1),
$q->start_html,
$q->pre(Dumper $status),
$q->end_html;
}
# send the user to Twitter to authorize us
sub authorize {
my ($self, $q) = @_;
my $auth_url = $self->twitter->get_authorization_url(callback => "$ENV{SERVER_URL}oauth_callback");
warn "Sending user to: $auth_url\n";
print $q->redirect(-nph => 1, -uri => $auth_url);
}
# Twitter returns the user here
sub oauth_callback {
my ($self, $q) = @_;
my $request_token = $q->param('oauth_token');
my $verifier = $q->param('oauth_verifier');
warn <<"";
User returned from Twitter with:
oauth_token => $request_token
oauth_verifier => $verifier
# exchange the request token for access tokens
my @access_tokens = $self->twitter->request_access_token(verifier => $verifier);
warn <<"";
Exchanged request tokens for access tokens:
access_token => $access_tokens[0]
access_token_secret => $access_tokens[1]
# we'll store the access tokens in a session cookie
my $cookie = $q->cookie(-name => 'sess', -value => {
access_token => $access_tokens[0],
access_token_secret => $access_tokens[1],
});
warn "redirecting newly authorized user to $ENV{SERVER_URL}\n";
print $q->redirect(-nph => 1, -uri => "$ENV{SERVER_URL}", -cookie => $cookie);
}
# display a 404 Not found for any request we don't expect
sub not_found {
my ($self, $q) = @_;
print $q->header(-nph => 1, -type => 'text/html', -status => '404 Not found'),
$q->start_html,
$q->h1('Not Found'),
$q->p('You appear to be lost. Try going home.');
}
my $app = MyWebApp->new($server_port);
$app->run;