line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package VK::OAuth; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
25389
|
use 5.010; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
42
|
|
5
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
659
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require LWP::UserAgent; |
9
|
|
|
|
|
|
|
require JSON; |
10
|
|
|
|
|
|
|
require Carp; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
17
|
0
|
|
|
|
|
|
my $self = bless {@_}, $class; |
18
|
|
|
|
|
|
|
|
19
|
0
|
0
|
0
|
|
|
|
Carp::croak("app_id, secret and postback required for this action") |
|
|
|
0
|
|
|
|
|
20
|
|
|
|
|
|
|
unless ($self->{app_id} && $self->{secret} && $self->{postback}); |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
0
|
|
|
|
$self->{ua} ||= LWP::UserAgent->new(); |
23
|
0
|
|
0
|
|
|
|
$self->{json} ||= JSON->new; |
24
|
0
|
|
|
|
|
|
return $self; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub authorize { |
29
|
0
|
|
|
0
|
1
|
|
my ($self, $params) = @_; |
30
|
|
|
|
|
|
|
|
31
|
0
|
0
|
0
|
|
|
|
my %params = %$params if $params && %$params; |
32
|
0
|
|
|
|
|
|
my $url = URI->new('https://oauth.vk.com/authorize'); |
33
|
0
|
|
|
|
|
|
$url->query_form( |
34
|
|
|
|
|
|
|
client_id => $self->{app_id}, |
35
|
|
|
|
|
|
|
response_type => 'code', |
36
|
|
|
|
|
|
|
redirect_uri => $self->{postback}, |
37
|
|
|
|
|
|
|
%params, |
38
|
|
|
|
|
|
|
); |
39
|
0
|
|
|
|
|
|
return $url; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub request_access_token { |
43
|
0
|
|
|
0
|
1
|
|
my ($self, $code) = @_; |
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
|
|
|
|
Carp::croak("code required for this action") unless ($code); |
46
|
0
|
|
|
|
|
|
my $url = URI->new('https://oauth.vk.com/access_token'); |
47
|
0
|
|
|
|
|
|
$url->query_form( |
48
|
|
|
|
|
|
|
'client_secret' => $self->{secret}, |
49
|
|
|
|
|
|
|
'client_id' => $self->{app_id}, |
50
|
|
|
|
|
|
|
'code' => $code, |
51
|
|
|
|
|
|
|
'redirect_uri' => $self->{postback}, |
52
|
|
|
|
|
|
|
); |
53
|
0
|
|
|
|
|
|
my $response = $self->{ua}->get($url); |
54
|
0
|
0
|
|
|
|
|
return 0 unless $response->is_success; |
55
|
0
|
|
|
|
|
|
my $obj = $self->{json}->decode($response->content); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# $obj->{user_id}, $obj->{access_token}, $obj->{expires_in} |
58
|
0
|
|
|
|
|
|
return $obj; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub request { |
62
|
0
|
|
|
0
|
1
|
|
my ( $self, $method, $access_token, $params ) = @_; |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
0
|
|
|
|
Carp::croak("method and access_token required for this action") |
65
|
|
|
|
|
|
|
unless ($method && $access_token); |
66
|
0
|
0
|
0
|
|
|
|
my %params = %$params if $params && %$params; |
67
|
0
|
|
|
|
|
|
my $url = URI->new('https://api.vk.com/method/' . $method); |
68
|
0
|
|
|
|
|
|
$url->query_form( |
69
|
|
|
|
|
|
|
access_token => $access_token, |
70
|
|
|
|
|
|
|
%params, |
71
|
|
|
|
|
|
|
); |
72
|
0
|
|
|
|
|
|
my $response = $self->{ua}->get($url); |
73
|
0
|
0
|
|
|
|
|
return 0 unless $response->is_success; |
74
|
0
|
|
|
|
|
|
my $obj = $self->{json}->decode($response->content); |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
return $obj; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
__END__ |