| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package API::Google; |
|
2
|
|
|
|
|
|
|
$API::Google::VERSION = '0.10'; |
|
3
|
|
|
|
|
|
|
# ABSTRACT: Perl library for easy access to Google services via their API |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
17585
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
29
|
|
|
7
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
24
|
|
|
8
|
1
|
|
|
1
|
|
482
|
use Mojo::UserAgent; |
|
|
1
|
|
|
|
|
276025
|
|
|
|
1
|
|
|
|
|
7
|
|
|
9
|
1
|
|
|
1
|
|
568
|
use Config::JSON; |
|
|
1
|
|
|
|
|
38014
|
|
|
|
1
|
|
|
|
|
28
|
|
|
10
|
1
|
|
|
1
|
|
8
|
use Data::Dumper; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
499
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
|
14
|
0
|
|
|
0
|
0
|
|
my ($class, $params) = @_; |
|
15
|
0
|
|
|
|
|
|
my $h = {}; |
|
16
|
0
|
0
|
|
|
|
|
if ($params->{tokensfile}) { |
|
17
|
0
|
|
|
|
|
|
$h->{tokensfile} = Config::JSON->new($params->{tokensfile}); |
|
18
|
|
|
|
|
|
|
} else { |
|
19
|
0
|
|
|
|
|
|
die 'no json file specified!'; |
|
20
|
|
|
|
|
|
|
} |
|
21
|
0
|
|
|
|
|
|
$h->{ua} = Mojo::UserAgent->new(); |
|
22
|
0
|
|
|
|
|
|
return bless $h, $class; |
|
23
|
|
|
|
|
|
|
} |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub refresh_access_token { |
|
28
|
0
|
|
|
0
|
0
|
|
my ($self, $params) = @_; |
|
29
|
0
|
|
|
|
|
|
warn Dumper $params; |
|
30
|
0
|
|
|
|
|
|
$params->{grant_type} = 'refresh_token'; |
|
31
|
0
|
|
|
|
|
|
$self->{ua}->post('https://www.googleapis.com/oauth2/v4/token' => form => $params)->res->json; # tokens |
|
32
|
|
|
|
|
|
|
}; |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub client_id { |
|
36
|
0
|
|
|
0
|
0
|
|
shift->{tokensfile}->get('gapi/client_id'); |
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub ua { |
|
40
|
0
|
|
|
0
|
0
|
|
shift->{ua}; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub client_secret { |
|
45
|
0
|
|
|
0
|
0
|
|
shift->{tokensfile}->get('gapi/client_secret'); |
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub refresh_access_token_silent { |
|
51
|
0
|
|
|
0
|
1
|
|
my ($self, $user) = @_; |
|
52
|
0
|
|
|
|
|
|
my $tokens = $self->refresh_access_token({ |
|
53
|
|
|
|
|
|
|
client_id => $self->client_id, |
|
54
|
|
|
|
|
|
|
client_secret => $self->client_secret, |
|
55
|
|
|
|
|
|
|
refresh_token => $self->get_refresh_token_from_storage($user) |
|
56
|
|
|
|
|
|
|
}); |
|
57
|
0
|
|
|
|
|
|
my $res = {}; |
|
58
|
0
|
|
|
|
|
|
$res->{old} = $self->get_access_token_from_storage($user); |
|
59
|
0
|
|
|
|
|
|
warn Dumper $tokens; |
|
60
|
0
|
0
|
|
|
|
|
if ($tokens->{access_token}) { |
|
61
|
0
|
|
|
|
|
|
$self->set_access_token_to_storage($user, $tokens->{access_token}); |
|
62
|
|
|
|
|
|
|
} |
|
63
|
0
|
|
|
|
|
|
$res->{new} = $self->get_access_token_from_storage($user); |
|
64
|
0
|
|
|
|
|
|
return $res; |
|
65
|
|
|
|
|
|
|
}; |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub get_refresh_token_from_storage { |
|
69
|
0
|
|
|
0
|
0
|
|
my ($self, $user) = @_; |
|
70
|
0
|
|
|
|
|
|
$self->{tokensfile}->get('gapi/tokens/'.$user.'/refresh_token'); |
|
71
|
|
|
|
|
|
|
}; |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub get_access_token_from_storage { |
|
74
|
0
|
|
|
0
|
0
|
|
my ($self, $user) = @_; |
|
75
|
0
|
|
|
|
|
|
$self->{tokensfile}->get('gapi/tokens/'.$user.'/access_token'); |
|
76
|
|
|
|
|
|
|
}; |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub set_access_token_to_storage { |
|
79
|
0
|
|
|
0
|
0
|
|
my ($self, $user, $token) = @_; |
|
80
|
0
|
|
|
|
|
|
$self->{tokensfile}->set('gapi/tokens/'.$user.'/access_token', $token); |
|
81
|
|
|
|
|
|
|
}; |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub api_query { |
|
87
|
0
|
|
|
0
|
1
|
|
my ($self, $params, $payload) = @_; |
|
88
|
|
|
|
|
|
|
my %headers = ( |
|
89
|
|
|
|
|
|
|
'Authorization' => 'Bearer '.$self->get_access_token_from_storage($params->{user}) |
|
90
|
0
|
|
|
|
|
|
); |
|
91
|
0
|
|
|
|
|
|
my $http_method = $params->{method}; |
|
92
|
0
|
0
|
0
|
|
|
|
if ($http_method eq 'get' || $http_method eq 'delete') { |
|
|
|
0
|
0
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
return $self->{ua}->$http_method($params->{route} => \%headers)->res->json; |
|
94
|
|
|
|
|
|
|
} elsif (($http_method eq 'post') && $payload) { |
|
95
|
0
|
|
|
|
|
|
return $self->{ua}->$http_method($params->{route} => \%headers => json => $payload)->res->json; |
|
96
|
|
|
|
|
|
|
} else { |
|
97
|
0
|
|
|
|
|
|
die 'wrong http_method'; |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
}; |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__END__ |