line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=encoding utf-8 |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Yandex::OAuth - module for get access token to Yandex.API |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Yandex::OAuth; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $oauth = Yandex::OAuth->new( |
12
|
|
|
|
|
|
|
client_id => '76df1cf****************fb31d0289', |
13
|
|
|
|
|
|
|
client_secret => 'e3a2855****************4de3c2afc', |
14
|
|
|
|
|
|
|
); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# return link for open in browser |
17
|
|
|
|
|
|
|
say $oauth->get_code(); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# return JSON with access_token |
20
|
|
|
|
|
|
|
say Dumper $oauth->get_token( code => 3557461 ); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Yandex::OAuth is a module for get access token for Yandex.API |
25
|
|
|
|
|
|
|
See more at https://tech.yandex.ru/oauth/doc/dg/concepts/ya-oauth-intro-docpage/ |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=cut |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
package Yandex::OAuth; |
31
|
2
|
|
|
2
|
|
1364
|
use 5.008001; |
|
2
|
|
|
|
|
8
|
|
32
|
2
|
|
|
2
|
|
1843
|
use utf8; |
|
2
|
|
|
|
|
20
|
|
|
2
|
|
|
|
|
10
|
|
33
|
2
|
|
|
2
|
|
1491
|
use Modern::Perl; |
|
2
|
|
|
|
|
25908
|
|
|
2
|
|
|
|
|
13
|
|
34
|
2
|
|
|
2
|
|
1375
|
use JSON::XS; |
|
2
|
|
|
|
|
5950
|
|
|
2
|
|
|
|
|
116
|
|
35
|
2
|
|
|
2
|
|
1433
|
use URI::Escape; |
|
2
|
|
|
|
|
2839
|
|
|
2
|
|
|
|
|
112
|
|
36
|
|
|
|
|
|
|
|
37
|
2
|
|
|
2
|
|
2139
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
88486
|
|
|
2
|
|
|
|
|
68
|
|
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
2
|
|
1611
|
use Moo; |
|
2
|
|
|
|
|
26226
|
|
|
2
|
|
|
|
|
13
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
our $VERSION = "0.07"; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
has 'ua' => ( |
44
|
|
|
|
|
|
|
is => 'ro', |
45
|
|
|
|
|
|
|
default => sub { |
46
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
47
|
|
|
|
|
|
|
$ua->agent( 'Yandex::OAuth Perl API Client' ); |
48
|
|
|
|
|
|
|
$ua->timeout( 120 ); |
49
|
|
|
|
|
|
|
return $ua; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has 'auth_url' => ( |
54
|
|
|
|
|
|
|
is => 'ro', |
55
|
|
|
|
|
|
|
default => 'https://oauth.yandex.ru/authorize', |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
has 'token_url' => ( |
60
|
|
|
|
|
|
|
is => 'ro', |
61
|
|
|
|
|
|
|
default => 'https://oauth.yandex.ru/token', |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
has 'client_id' => ( |
65
|
|
|
|
|
|
|
is => 'rw', |
66
|
|
|
|
|
|
|
required => 1, |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
has 'client_secret' => ( |
70
|
|
|
|
|
|
|
is => 'rw', |
71
|
|
|
|
|
|
|
required => 1, |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
has 'demo' => ( |
75
|
|
|
|
|
|
|
is => 'rw' |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 METHODS |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=over |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=item B |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
return a link for open in browser |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
$oauth->get_code(); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub get_code { |
91
|
2
|
|
|
2
|
1
|
13
|
my ( $self, %params ) = @_; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
return $self->auth_url . "?response_type=code&client_id=".$self->client_id . |
94
|
2
|
100
|
|
|
|
34
|
( ( defined $params{state} ) ? "&state=" . uri_escape( $params{state} ) : '' ); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=item B |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
return a json with access_token or error if code has expired |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
$oauth->get_token( code => XXXXXX ); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub get_token { |
106
|
1
|
|
|
1
|
1
|
8
|
my ( $self, %params ) = @_; |
107
|
|
|
|
|
|
|
|
108
|
1
|
50
|
|
|
|
62
|
return JSON::XS::decode_json( $self->demo ) if $self->demo; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $res = $self->ua->post($self->token_url, { |
111
|
|
|
|
|
|
|
code => $params{code}, |
112
|
0
|
|
|
|
|
|
client_id => $self->client_id, |
113
|
|
|
|
|
|
|
client_secret => $self->client_secret, |
114
|
|
|
|
|
|
|
grant_type => 'authorization_code', |
115
|
|
|
|
|
|
|
}); |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
return JSON::XS::decode_json( $res->content ); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
1; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
__END__ |