| 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 | ▶ | use 5.008001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 2 | ▶ | use Modern::Perl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 2 | ▶ | use JSON::XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 2 | ▶ | use URI::Escape; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 2 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | |||||||||||
| 41 | 2 | our $VERSION = "0.05"; | |||||||||
| 42 | |||||||||||
| 43 | has 'ua' => ( | ||||||||||
| 44 | is => 'ro', | ||||||||||
| 45 | default => sub { | ||||||||||
| 46 | 2 | ▶ | my $ua = LWP::UserAgent->new(); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 2 | $ua->agent( 'Yandex::OAuth Perl API Client' ); | |||||||||
| 48 | 2 | $ua->timeout( 120 ); | |||||||||
| 49 | 2 | return $ua; | |||||||||
| 50 | } | ||||||||||
| 51 | 2 | ); | |||||||||
| 52 | |||||||||||
| 53 | 2 | has 'auth_url' => ( | |||||||||
| 54 | is => 'ro', | ||||||||||
| 55 | default => 'https://oauth.yandex.ru/authorize', | ||||||||||
| 56 | |||||||||||
| 57 | ); | ||||||||||
| 58 | |||||||||||
| 59 | 2 | has 'token_url' => ( | |||||||||
| 60 | is => 'ro', | ||||||||||
| 61 | default => 'https://oauth.yandex.ru/token', | ||||||||||
| 62 | ); | ||||||||||
| 63 | |||||||||||
| 64 | 2 | has 'client_id' => ( | |||||||||
| 65 | is => 'rw', | ||||||||||
| 66 | required => 1, | ||||||||||
| 67 | ); | ||||||||||
| 68 | |||||||||||
| 69 | 2 | has 'client_secret' => ( | |||||||||
| 70 | is => 'rw', | ||||||||||
| 71 | required => 1, | ||||||||||
| 72 | ); | ||||||||||
| 73 | |||||||||||
| 74 | 2 | has 'demo' => ( | |||||||||
| 75 | is => 'rw' | ||||||||||
| 76 | ); | ||||||||||
| 77 | |||||||||||
| 78 | =head1 METHODS | ||||||||||
| 79 | |||||||||||
| 80 | =over | ||||||||||
| 81 | |||||||||||
| 82 | =item B<get_code()> | ||||||||||
| 83 | |||||||||||
| 84 | return a link for open in browser | ||||||||||
| 85 | |||||||||||
| 86 | $oauth->get_code(); | ||||||||||
| 87 | |||||||||||
| 88 | =cut | ||||||||||
| 89 | |||||||||||
| 90 | sub get_code { | ||||||||||
| 91 | 2 | ▶ | my ( $self, %params ) = @_; | ||||||||
|
Subroutine
get_code: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 92 | |||||||||||
| 93 | return $self->auth_url . "?response_type=code&client_id=".$self->client_id . | ||||||||||
| 94 | 2 | ▶ | ( ( defined $params{state} ) ? "&state=" . uri_escape( $params{state} ) : '' ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | =item B<get_token()> | ||||||||||
| 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 | ▶ | my ( $self, %params ) = @_; | ||||||||
|
Subroutine
get_token: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||
| 107 | |||||||||||
| 108 | 1 | ▶ | return JSON::XS::decode_json( $self->demo ) if $self->demo; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 2 | 1; | |||||||||
| 121 | |||||||||||
| 122 | __END__ | ||||||||||