line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package OAuth::Simple; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
30908
|
use 5.010; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
43
|
|
4
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
14
|
|
|
1
|
|
|
|
|
32
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
1622
|
use HTTP::Request::Common; |
|
1
|
|
|
|
|
43536
|
|
|
1
|
|
|
|
|
1145
|
|
8
|
|
|
|
|
|
|
require LWP::UserAgent; |
9
|
|
|
|
|
|
|
require JSON; |
10
|
|
|
|
|
|
|
require Carp; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '1.01'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
17
|
0
|
|
|
|
|
|
my $self = bless {@_}, $class; |
18
|
|
|
|
|
|
|
|
19
|
0
|
0
|
0
|
|
|
|
Carp::croak("app_id, secret and postback required for this action") |
|
|
|
0
|
|
|
|
|
20
|
|
|
|
|
|
|
unless ($self->{app_id} && $self->{secret} && $self->{postback}); |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
0
|
|
|
|
$self->{ua} ||= LWP::UserAgent->new(); |
23
|
0
|
|
0
|
|
|
|
$self->{json} ||= JSON->new; |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
return $self; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub authorize { |
30
|
0
|
|
|
0
|
1
|
|
my ($self, $params) = @_; |
31
|
|
|
|
|
|
|
|
32
|
0
|
0
|
0
|
|
|
|
my %params; %params = %$params if $params && %$params; |
|
0
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
my $url = delete $params{url}; |
35
|
0
|
0
|
|
|
|
|
Carp::croak("Authorize method URL required for this action") unless ($url); |
36
|
0
|
|
|
|
|
|
$url = URI->new($url); |
37
|
0
|
|
|
|
|
|
$url->query_form( |
38
|
|
|
|
|
|
|
client_id => $self->{app_id}, |
39
|
|
|
|
|
|
|
redirect_uri => $self->{postback}, |
40
|
|
|
|
|
|
|
%params, |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
return $url; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub request_access_token { |
47
|
0
|
|
|
0
|
1
|
|
my ( $self, $params ) = @_; |
48
|
|
|
|
|
|
|
|
49
|
0
|
0
|
0
|
|
|
|
my %params; %params = %$params if $params && %$params; |
|
0
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my ( $url, $code, $raw, $http_method ) = delete @params{ qw(url code raw http_method) }; |
52
|
0
|
0
|
0
|
|
|
|
Carp::croak("code and url required for this action") unless $code && $url; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
my $response = $self->{ua}->request($self->prepare_http_request( |
55
|
|
|
|
|
|
|
url => $url, |
56
|
|
|
|
|
|
|
http_method => $http_method, |
57
|
|
|
|
|
|
|
params => { |
58
|
|
|
|
|
|
|
client_secret => $self->{secret}, |
59
|
|
|
|
|
|
|
client_id => $self->{app_id}, |
60
|
|
|
|
|
|
|
code => $code, |
61
|
|
|
|
|
|
|
redirect_uri => $self->{postback}, |
62
|
|
|
|
|
|
|
%params, |
63
|
|
|
|
|
|
|
}, |
64
|
|
|
|
|
|
|
)); |
65
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
|
|
|
return $response->decoded_content unless $response->is_success; |
67
|
0
|
0
|
|
|
|
|
return $response->content if $raw; |
68
|
0
|
|
|
|
|
|
return $self->{json}->decode($response->content); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub request_data { |
72
|
0
|
|
|
0
|
1
|
|
my ( $self, $params ) = @_; |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
0
|
|
|
|
my %params; %params = %$params if $params && %$params; |
|
0
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
my ( $url, $access_token, $raw, $http_method, $token_name ) = |
77
|
|
|
|
|
|
|
delete @params{ qw(url access_token raw http_method token_name) }; |
78
|
0
|
0
|
0
|
|
|
|
Carp::croak("url and access_token required for this action") |
79
|
|
|
|
|
|
|
unless ($url && $access_token); |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
0
|
|
|
|
my $response = $self->{ua}->request($self->prepare_http_request( |
82
|
|
|
|
|
|
|
url => $url, |
83
|
|
|
|
|
|
|
http_method => $http_method, |
84
|
|
|
|
|
|
|
params => { |
85
|
|
|
|
|
|
|
($token_name || 'access_token') => $access_token, |
86
|
|
|
|
|
|
|
%params |
87
|
|
|
|
|
|
|
}, |
88
|
|
|
|
|
|
|
)); |
89
|
|
|
|
|
|
|
|
90
|
0
|
0
|
|
|
|
|
return 0 unless $response->is_success; |
91
|
0
|
0
|
|
|
|
|
return $response->content if $raw; |
92
|
0
|
|
|
|
|
|
return $self->{json}->decode($response->content); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub prepare_http_request { |
96
|
0
|
|
|
0
|
1
|
|
my ( $self, %params ) = @_; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
0
|
|
|
|
$params{http_method} ||= 'GET'; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
my $req; |
101
|
0
|
0
|
|
|
|
|
if ($params{http_method} eq 'GET') { |
102
|
0
|
|
|
|
|
|
my $url = URI->new($params{url}); |
103
|
0
|
0
|
|
|
|
|
$url->query_form( %{$params{params}} ) if $params{params}; |
|
0
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
$req = GET $url; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
else { |
107
|
0
|
|
|
|
|
|
$req = POST $params{url}, $params{params}; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
return $req; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |