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