| 1 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # Google::ContentAPI | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # Add, modify and delete items from the Google Merchant Center platform via | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # the Content API for Shopping. | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # https://developers.google.com/shopping-content/v2/quickstart | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # Authentication is done via Service Account credentials. For details see: | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # https://developers.google.com/shopping-content/v2/how-tos/service-accounts | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # Bill Gerrard <bill@gerrard.org> | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # VERSION HISTORY | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # + v1.03 12/10/2020 Allow custom endpoint, update docs for v2.1 API | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # + v1.02 04/11/2018 Add accountstatuses, productstatuses methods. Add "custom" resource | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # + v1.01 03/27/2018 Added config_json, merchant_id options and switched to Crypt::JWT | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # + v1.00 03/23/2018 initial release | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # Copyright (C) 2018,2020 Bill Gerrard | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # it under the same terms as Perl itself, either Perl version 5.20.2 or, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # Disclaimer of warranty: This program is provided by the copyright holder | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # and contributors "As is" and without any express or implied warranties. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # The implied warranties of merchantability, fitness for a particular purpose, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # or non-infringement are disclaimed to the extent permitted by your local | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # law. Unless required by law, no copyright holder or contributor will be | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # liable for any direct, indirect, incidental, or consequential damages | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # arising in any way out of the use of the package, even if advised of the | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # possibility of such damage. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # | ||||||||||||||||||||||||||||||||||||||||||
| 40 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | package Google::ContentAPI; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use Crypt::JWT qw(encode_jwt); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | use REST::Client; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | our $VERSION = '1.03'; | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my ($class, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ($params->{config_file}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $self->{config} = load_google_config($params->{config_file}); | |||||||||||||||||||||||||||||||||||||||||
| 61 | } elsif ($params->{config_json}) { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $self->{config} = decode_json $params->{config_json}; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | croak "config_file or config_json not provided in new()"; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | $self->{merchant_id} = $self->{config}->{merchant_id} | ||||||||||||||||||||||||||||||||||||||||||
| 67 | || $params->{merchant_id} | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | || croak "'merchant_id' not provided in json config in new()"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$self->{'config'}{'merchant_id'} || $params->{'merchant_id'} || croak(q['merchant_id' not provided in json config in new()])
$self->{"config"}{"merchant_id"} or $params->{"merchant_id"}
MC/DC0%0/2
$self->{'config'}{'merchant_id'} || $params->{'merchant_id'} || croak(q['merchant_id' not provided in json config in new()])
$self->{"config"}{"merchant_id"}
$params->{"merchant_id"}
Truth table0%0/3
A or B
A= $self->{"config"}{"merchant_id"}B= $params->{"merchant_id"}
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | $self->{endpoint} = $self->{config}->{endpoint} | ||||||||||||||||||||||||||||||||||||||||||
| 71 | || $params->{endpoint} | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | || 'https://www.googleapis.com/content/v2'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$self->{'config'}{'endpoint'} || $params->{'endpoint'} || 'https://www.googleapis.com/content/v2'
$self->{"config"}{"endpoint"} or $params->{"endpoint"}
MC/DC0%0/2
$self->{'config'}{'endpoint'} || $params->{'endpoint'} || 'https://www.googleapis.com/content/v2'
$self->{"config"}{"endpoint"}
$params->{"endpoint"}
Truth table0%0/3
A or B
A= $self->{"config"}{"endpoint"}B= $params->{"endpoint"}
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $self->{debug} = 1 if $params->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $self->{google_auth_token} = get_google_auth_token($self); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $self->{rest} = init_rest_client($self); | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | return bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | croak "Odd number of arguments for get()" if scalar(@_) % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $opt = {@_}; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $method = $self->prepare_method($opt); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | return $self->request('GET', $method); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub post { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | croak "Odd number of arguments for post()" if scalar(@_) % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my $opt = {@_}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my $method = $self->prepare_method($opt); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $opt->{body} = encode_json $opt->{body} if $opt->{body}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | return $self->request('POST', $method, $opt->{body}); | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | croak "Odd number of arguments for delete()" if scalar(@_) % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $opt = {@_}; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $method = $self->prepare_method($opt); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | return $self->request('DELETE', $method); | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub prepare_method { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_method: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $opt = shift; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $opt->{resource} = '' if $opt->{resource} eq 'custom'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ($opt->{resource} eq 'products' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$opt->{"resource"} eq "products" or $opt->{"resource"} eq "productstatuses" or $opt->{"resource"} eq "accountstatuses"
$opt->{"resource"} eq "products" or $opt->{"resource"} eq "productstatuses"
MC/DC0%0/3
$opt->{"resource"} eq "products" or $opt->{"resource"} eq "productstatuses" or $opt->{"resource"} eq "accountstatuses"
$opt->{"resource"} eq "products"
$opt->{"resource"} eq "productstatuses"
$opt->{"resource"} eq "accountstatuses"
Truth table0%0/4
A or B or C
A= $opt->{"resource"} eq "products"B= $opt->{"resource"} eq "productstatuses"C= $opt->{"resource"} eq "accountstatuses"
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | || $opt->{resource} eq 'productstatuses' | ||||||||||||||||||||||||||||||||||||||||||
| 114 | || $opt->{resource} eq 'accountstatuses' | ||||||||||||||||||||||||||||||||||||||||||
| 115 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # add merchant ID to request URL for non-batch requests | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | $opt->{resource} = $self->{merchant_id} .'/'. $opt->{resource} if $opt->{method} ne 'batch'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | # drop list/insert methods; these are for coding convenience only | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $opt->{method} = '' if $opt->{method} eq 'list'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $opt->{method} = '' if $opt->{method} eq 'insert'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | # append product ID to end of request URL for get and delete | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | $opt->{method} = $opt->{id} if $opt->{method} =~ /get|delete/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | my $encoded_params = $self->{rest}->buildQuery($opt->{params}) if $opt->{params}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | my $method; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | $method .= '/'. $opt->{resource} if $opt->{resource} ne ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $method .= '/'. $opt->{method} if $opt->{method} ne ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $method .= $encoded_params if $encoded_params; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | return $method; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub init_rest_client { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init_rest_client: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my $r = REST::Client->new(); | |||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $r->setHost($self->{endpoint}); | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | $r->addHeader('Authorization', $self->{google_auth_token}); | |||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | $r->addHeader('Content-type', 'application/json'); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $r->addHeader('charset', 'UTF-8'); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | return $r; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my @command = @_; | |||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | print join (' ', @command) . "\n" if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | my $rest = $self->{rest}->request(@command); | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | unless ($rest->responseCode eq '200') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | if ($rest->responseCode eq '204' && $command[0] eq 'DELETE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$rest->responseCode eq 204 and $command[0] eq "DELETE"
MC/DC0%0/2
$rest->responseCode eq 204 and $command[0] eq "DELETE"
$rest->responseCode eq 204
$command[0] eq "DELETE"
Truth table0%0/3
A and B
A= $rest->responseCode eq 204B= $command[0] eq "DELETE"
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | # no-op: delete was successful | ||||||||||||||||||||||||||||||||||||||||||
| 155 | } elsif ($rest->responseCode eq '401') { | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # access token expired, request new token and retry request | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | $self->{google_auth_token} = $self->get_google_auth_token(); | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | $self->{rest} = $self->init_rest_client(); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | $rest = $self->{rest}->request(@command); | |||||||||||||||||||||||||||||||||||||||||
| 160 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | die("Error processing REST request:\n", | |||||||||||||||||||||||||||||||||||||||||
| 162 | "Request: ", $rest->getHost , $command[1], "\n", | ||||||||||||||||||||||||||||||||||||||||||
| 163 | "Response Code: ", $rest->responseCode, "\n", $rest->responseContent, "\n"); | ||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | print "Request Response: \n". $rest->responseContent if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | my $response = $rest->responseContent ? decode_json $rest->responseContent : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | return { code => $rest->responseCode, response => $response }; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub get_google_auth_token { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_google_auth_token: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my $gapiTokenURI = 'https://www.googleapis.com/oauth2/v4/token'; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $gapiContentScope = 'https://www.googleapis.com/auth/content'; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my $time = time(); | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | # 1) Create JSON Web Token | ||||||||||||||||||||||||||||||||||||||||||
| 180 | # https://developers.google.com/accounts/docs/OAuth2ServiceAccount | ||||||||||||||||||||||||||||||||||||||||||
| 181 | # | ||||||||||||||||||||||||||||||||||||||||||
| 182 | my $jwt = encode_jwt( | ||||||||||||||||||||||||||||||||||||||||||
| 183 | payload => { | ||||||||||||||||||||||||||||||||||||||||||
| 184 | iss => $self->{config}->{client_email}, | ||||||||||||||||||||||||||||||||||||||||||
| 185 | scope => $gapiContentScope, | ||||||||||||||||||||||||||||||||||||||||||
| 186 | aud => $gapiTokenURI, | ||||||||||||||||||||||||||||||||||||||||||
| 187 | exp => $time + 3660, # max 60 minutes | ||||||||||||||||||||||||||||||||||||||||||
| 188 | iat => $time | ||||||||||||||||||||||||||||||||||||||||||
| 189 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 190 | key => \$self->{config}->{private_key}, | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | alg => 'RS256' | |||||||||||||||||||||||||||||||||||||||||
| 192 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | # 2) Request an access token | ||||||||||||||||||||||||||||||||||||||||||
| 195 | # | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | my $response = $ua->post($gapiTokenURI, { | |||||||||||||||||||||||||||||||||||||||||
| 198 | grant_type => encode_entities('urn:ietf:params:oauth:grant-type:jwt-bearer'), | ||||||||||||||||||||||||||||||||||||||||||
| 199 | assertion => $jwt | ||||||||||||||||||||||||||||||||||||||||||
| 200 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | print "Request Access Token response:\n". $response->content if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | unless($response->is_success()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | die("Error receiving access token:\n", $response->code, "\n", $response->content, "\n"); | |||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | my $data = decode_json $response->content; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | return 'Bearer '. $data->{access_token}; | |||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub load_google_config { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_google_config: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | my $json_file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | open my $fh, '<', $json_file or croak "Error reading config_file '$json_file': $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | my $json_text; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | { local $/; $json_text = <$fh>; } | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | return decode_json $json_text; | |||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | __END__ | ||||||||||||||||||||||||||||||||||||||||||