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
|
|
1000
|
use 5.008001; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
62
|
|
32
|
2
|
|
|
2
|
|
908
|
use utf8; |
|
2
|
|
|
|
|
15
|
|
|
2
|
|
|
|
|
6
|
|
33
|
2
|
|
|
2
|
|
788
|
use Modern::Perl; |
|
2
|
|
|
|
|
15043
|
|
|
2
|
|
|
|
|
11
|
|
34
|
2
|
|
|
2
|
|
1096
|
use JSON::XS; |
|
2
|
|
|
|
|
3199
|
|
|
2
|
|
|
|
|
82
|
|
35
|
2
|
|
|
2
|
|
713
|
use URI::Escape; |
|
2
|
|
|
|
|
1640
|
|
|
2
|
|
|
|
|
81
|
|
36
|
|
|
|
|
|
|
|
37
|
2
|
|
|
2
|
|
962
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
69997
|
|
|
2
|
|
|
|
|
55
|
|
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
2
|
|
1715
|
use Moo; |
|
2
|
|
|
|
|
18200
|
|
|
2
|
|
|
|
|
9
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
our $VERSION = "0.05"; |
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
|
11
|
my ( $self, %params ) = @_; |
92
|
|
|
|
|
|
|
|
93
|
2
|
100
|
|
|
|
30
|
return $self->auth_url . "?response_type=code&client_id=".$self->client_id . |
94
|
|
|
|
|
|
|
( ( 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
|
6
|
my ( $self, %params ) = @_; |
107
|
|
|
|
|
|
|
|
108
|
1
|
50
|
|
|
|
32
|
return JSON::XS::decode_json( $self->demo ) if $self->demo; |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
my $res = $self->ua->post($self->token_url, { |
111
|
|
|
|
|
|
|
code => $params{code}, |
112
|
|
|
|
|
|
|
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__ |