line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Twitch::Oauth2; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
16915
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
65
|
|
4
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
45
|
|
5
|
2
|
|
|
2
|
|
1198
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
80366
|
|
|
2
|
|
|
|
|
70
|
|
6
|
2
|
|
|
2
|
|
13
|
use URI; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
34
|
|
7
|
2
|
|
|
2
|
|
7
|
use URI::Escape; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
128
|
|
8
|
2
|
|
|
2
|
|
1016
|
use JSON::Any; |
|
2
|
|
|
|
|
31183
|
|
|
2
|
|
|
|
|
10
|
|
9
|
2
|
|
|
2
|
|
7712
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
155
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
12
|
use constant ACCESS_TOKEN_URL => 'https://api.twitch.tv/kraken/oauth2/token'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
115
|
|
12
|
2
|
|
|
2
|
|
8
|
use constant AUTHORIZE_URL => 'https://api.twitch.tv/kraken/oauth2/authorize'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1735
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
6
|
|
|
6
|
0
|
4484
|
my ($class,%options) = @_; |
18
|
6
|
|
|
|
|
12
|
my $self = {}; |
19
|
6
|
|
|
|
|
9
|
$self->{options} = \%options; |
20
|
|
|
|
|
|
|
|
21
|
6
|
100
|
|
|
|
14
|
if (!$options{access_token}){ |
22
|
3
|
100
|
|
|
|
16
|
croak "You must provide your application id when construct new method\n EPI::Twitch::Oauth2->new( application_id => '...' )" unless defined $self->{options}->{application_id}; |
23
|
2
|
100
|
|
|
|
25
|
croak "You must provide your application secret when construct new method\n EPI::Twitch::Oauth2->new( application_secret => '...' )" unless defined $self->{options}->{application_secret}; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
4
|
|
66
|
|
|
26
|
$self->{browser} = $options{browser} || LWP::UserAgent->new; |
27
|
4
|
|
50
|
|
|
2092
|
$self->{access_token_url} = $options{access_token_url} || ACCESS_TOKEN_URL; |
28
|
4
|
|
50
|
|
|
14
|
$self->{authorize_url} = $options{authorize_url} || AUTHORIZE_URL; |
29
|
4
|
|
|
|
|
7
|
$self->{access_token} = $options{access_token}; |
30
|
|
|
|
|
|
|
|
31
|
4
|
|
|
|
|
13
|
return bless($self, $class); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub get_authorization_url { |
35
|
0
|
|
|
0
|
0
|
0
|
my ($self,%params) = @_; |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
0
|
|
|
0
|
$params{callback} ||= $self->{options}->{callback}; |
38
|
0
|
0
|
|
|
|
0
|
croak "You must pass a callback parameter with Oauth v2.0" unless defined $params{callback}; |
39
|
|
|
|
|
|
|
|
40
|
0
|
0
|
|
|
|
0
|
my $scope = join(",", @{$params{scope}}) if defined($params{scope}); |
|
0
|
|
|
|
|
0
|
|
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
0
|
my $url = $self->{authorize_url} |
43
|
|
|
|
|
|
|
.'?response_type=code' |
44
|
|
|
|
|
|
|
.'&client_id=' |
45
|
|
|
|
|
|
|
.uri_escape($self->{options}->{application_id}) |
46
|
|
|
|
|
|
|
.'&redirect_uri=' |
47
|
|
|
|
|
|
|
.uri_escape($params{callback}); |
48
|
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
0
|
$url .= "&scope=$scope" if $scope; |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
0
|
return $url; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub post_access_token { |
55
|
0
|
|
|
0
|
0
|
0
|
my ($self,%params) = @_; |
56
|
0
|
|
0
|
|
|
0
|
$params{callback} ||= $self->{options}->{callback}; |
57
|
0
|
|
0
|
|
|
0
|
$params{code} ||= $self->{options}->{code}; |
58
|
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
0
|
croak "You must pass a code parameter with Oauth v2.0" unless defined $params{code}; |
60
|
0
|
0
|
|
|
|
0
|
croak "You must pass callback URL" unless defined $params{callback}; |
61
|
0
|
|
|
|
|
0
|
$self->{options}->{code} = $params{code}; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
###generating access token URL |
64
|
0
|
|
|
|
|
0
|
my $getURL = $self->{access_token_url} |
65
|
|
|
|
|
|
|
.'?client_id=' |
66
|
|
|
|
|
|
|
.uri_escape($self->{options}->{application_id}) |
67
|
|
|
|
|
|
|
.'&client_secret=' |
68
|
|
|
|
|
|
|
.uri_escape($self->{options}->{application_secret}) |
69
|
|
|
|
|
|
|
.'&grant_type=authorization_code' |
70
|
|
|
|
|
|
|
.'&redirect_uri=' |
71
|
|
|
|
|
|
|
.uri_escape($params{callback}) |
72
|
|
|
|
|
|
|
.'&code=' |
73
|
|
|
|
|
|
|
.uri_escape($params{code}); |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
0
|
my $response = $self->{browser}->post($getURL); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
##got an error response from twitch |
78
|
|
|
|
|
|
|
##die and display error message |
79
|
0
|
|
|
|
|
0
|
my $j = JSON::Any->new; |
80
|
0
|
0
|
|
|
|
0
|
if (!$response->is_success){ |
81
|
0
|
|
|
|
|
0
|
my $error = $j->jsonToObj($response->content()); |
82
|
0
|
|
|
|
|
0
|
croak "'" .$error->{error}->{type}. "'" . " " .$error->{error}->{message}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
##everything is ok proccess response and extract access token |
86
|
0
|
|
|
|
|
0
|
my $reply = $j->jsonToObj($response->content()); |
87
|
0
|
|
|
|
|
0
|
my $token = $reply->{access_token}; |
88
|
0
|
|
|
|
|
0
|
my $expires = $reply->{refresh_token}; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
###save access token |
91
|
0
|
0
|
|
|
|
0
|
if ($token){ |
92
|
0
|
|
|
|
|
0
|
$self->{access_token} = $token; |
93
|
0
|
|
|
|
|
0
|
return $token; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
0
|
croak "can't get access token"; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub get { |
100
|
2
|
|
|
2
|
0
|
8
|
my ($self,$url,$params) = @_; |
101
|
2
|
50
|
|
|
|
12
|
unless ($self->_has_access_token($url)) { |
102
|
2
|
50
|
|
|
|
6
|
croak "You must pass access_token" unless defined $self->{access_token}; |
103
|
2
|
50
|
|
|
|
7
|
$url .= $self->{_has_query} ? '&' : '?'; |
104
|
2
|
|
|
|
|
5
|
$url .= "oauth_token=" . $self->{access_token}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
##construct the new url |
108
|
2
|
|
|
|
|
2
|
my @array; |
109
|
|
|
|
|
|
|
|
110
|
2
|
|
|
|
|
3
|
while ( my ($key, $value) = each(%{$params})){ |
|
2
|
|
|
|
|
7
|
|
111
|
0
|
|
|
|
|
0
|
$value = uri_escape($value); |
112
|
0
|
|
|
|
|
0
|
push(@array, "$key=$value"); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
2
|
|
|
|
|
6
|
my $string = join('&', @array); |
116
|
2
|
50
|
|
|
|
4
|
$url .= "&".$string if $string; |
117
|
|
|
|
|
|
|
|
118
|
2
|
|
|
|
|
14
|
my $response = $self->{browser}->get($url); |
119
|
2
|
|
|
|
|
109
|
my $content = $response->content(); |
120
|
2
|
|
|
|
|
66
|
return $self->_content($content); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub post { |
124
|
0
|
|
|
0
|
0
|
0
|
my ($self,$url,$params) = @_; |
125
|
0
|
0
|
|
|
|
0
|
unless ($self->_has_access_token($url)) { |
126
|
0
|
0
|
|
|
|
0
|
croak "You must pass access_token" unless defined $self->{access_token}; |
127
|
0
|
|
|
|
|
0
|
$params->{oauth_token} = $self->{access_token}; |
128
|
|
|
|
|
|
|
} |
129
|
0
|
|
|
|
|
0
|
my $response = $self->{browser}->post($url,$params); |
130
|
0
|
|
|
|
|
0
|
my $content = $response->content(); |
131
|
0
|
|
|
|
|
0
|
return $self->_content($content); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub delete { |
135
|
1
|
|
|
1
|
0
|
5
|
my ($self,$url,$params) = @_; |
136
|
1
|
50
|
|
|
|
3
|
unless ($self->_has_access_token($url)) { |
137
|
1
|
50
|
|
|
|
3
|
croak "You must pass access_token" unless defined $self->{access_token}; |
138
|
1
|
|
|
|
|
3
|
$params->{oauth_token} = $self->{access_token}; |
139
|
|
|
|
|
|
|
} |
140
|
1
|
|
|
|
|
8
|
my $response = $self->{browser}->delete($url,$params); |
141
|
1
|
|
|
|
|
48
|
my $content = $response->content(); |
142
|
1
|
|
|
|
|
46
|
return $self->_content($content); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub as_hash { |
146
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
147
|
0
|
|
|
|
|
0
|
my $j = JSON::Any->new; |
148
|
0
|
|
|
|
|
0
|
return $j->jsonToObj($self->{content}); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub as_json { |
152
|
3
|
|
|
3
|
0
|
3
|
my ($self) = @_; |
153
|
3
|
|
|
|
|
13
|
return $self->{content}; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _content { |
157
|
3
|
|
|
3
|
|
5
|
my ($self,$content) = @_; |
158
|
3
|
|
|
|
|
4
|
$self->{content} = $content; |
159
|
3
|
|
|
|
|
11
|
return $self; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub _has_access_token { |
163
|
3
|
|
|
3
|
|
3
|
my ($self, $url) = @_; |
164
|
3
|
|
|
|
|
17
|
my $uri = URI->new($url); |
165
|
3
|
|
|
|
|
3992
|
my %q = $uri->query_form; |
166
|
|
|
|
|
|
|
#also check if we have a query and save result |
167
|
3
|
|
|
|
|
101
|
$self->{_has_query} = $uri->query(); |
168
|
3
|
50
|
|
|
|
26
|
if (grep { $_ eq 'oauth_token' } keys %q) { |
|
0
|
|
|
|
|
0
|
|
169
|
0
|
|
|
|
|
0
|
return 1; |
170
|
|
|
|
|
|
|
} |
171
|
3
|
|
|
|
|
16
|
return; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
1; |