API-Google
view release on metacpan or search on metacpan
2. You may apply bug fixes, portability fixes and other modifications derived
from the Public Domain or from the Copyright Holder. A Package modified in such
a way shall still be considered the Standard Version.
3. You may otherwise modify your copy of this Package in any way, provided that
you insert a prominent notice in each changed file stating how and when you
changed that file, and provided that you do at least ONE of the following:
a) place your modifications in the Public Domain or otherwise make them
Freely Available, such as by posting said modifications to Usenet or an
equivalent medium, or placing the modifications on a major archive site
such as ftp.uu.net, or by allowing the Copyright Holder to include your
modifications in the Standard Version of the Package.
b) use the modified Package only within your corporation or organization.
c) rename any non-standard executables so the names do not conflict with
standard executables, which must also be provided, and provide a separate
manual page for each non-standard executable that clearly documents how it
differs from the Standard Version.
version 0.12
# SYNOPSIS
use API::Google;
my $gapi = API::Google->new({ tokensfile => 'config.json' });
$gapi->refresh_access_token_silent('someuser@gmail.com');
$gapi->api_query({
method => 'post',
route => 'https://www.googleapis.com/calendar/v3/calendars/'.$calendar_id.'/events',
user => 'someuser@gmail.com'
}, $json_payload_if_post);
# CONFIGURATION
config.json must be structured like:
{ "gapi":
{
"client_id": "001122334455-abcdefghijklmnopqrstuvwxyz012345.apps.googleusercontent.com",
"client_secret": "1ayL76NlEKjj85eZOipFZkyM",
"tokens": {
## build\_headers
Keep access\_token in headers always actual
$gapi->build\_http\_transactio($user);
## build\_http\_transaction
$gapi->build\_http\_transaction({
user => 'someuser@gmail.com',
method => 'post',
route => 'https://www.googleapis.com/calendar/users/me/calendarList',
payload => { key => value }
})
## api\_query
Low-level method that can make API query to any Google service
Required params: method, route, user
Examples of usage:
$gapi->api_query({
method => 'get',
route => 'https://www.googleapis.com/calendar/users/me/calendarList'',
user => 'someuser@gmail.com'
});
$gapi->api_query({
method => 'post',
route => 'https://www.googleapis.com/calendar/v3/calendars/'.$calendar_id.'/events',
user => 'someuser@gmail.com'
}, $json_payload_if_post);
# AUTHOR
Pavel Serikov <pavelsr@cpan.org>
# COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by Pavel Serikov.
This is free software; you can redistribute it and/or modify it under
lib/API/Google.pm view on Meta::CPAN
$h->{max_refresh_attempts} = $params->{max_refresh_attempts} || 5;
return bless $h, $class;
}
sub refresh_access_token {
my ($self, $params) = @_;
warn "Attempt to refresh access_token with params: ".Dumper $params if $self->{debug};
$params->{grant_type} = 'refresh_token';
$self->{ua}->post('https://www.googleapis.com/oauth2/v4/token' => form => $params)->res->json; # tokens
};
sub client_id {
shift->{tokensfile}->get('gapi/client_id');
}
sub ua {
shift->{ua};
}
lib/API/Google.pm view on Meta::CPAN
my ($self, $params) = @_;
warn "build_http_transaction() params : ".Dumper $params if $self->{debug};
my $headers = $self->build_headers($params->{user});
my $http_method = $params->{method};
my $tx;
if ($http_method eq 'get' || $http_method eq 'delete') {
$tx = $self->{ua}->build_tx(uc $http_method => $params->{route} => $headers);
} elsif (($http_method eq 'post') && $params->{payload}) {
$tx = $self->{ua}->build_tx(uc $http_method => $params->{route} => $headers => json => $params->{payload})
} else {
die 'wrong http_method on no payload if using POST';
}
return $tx;
}
lib/API/Google.pm view on Meta::CPAN
version 0.12
=head1 SYNOPSIS
use API::Google;
my $gapi = API::Google->new({ tokensfile => 'config.json' });
$gapi->refresh_access_token_silent('someuser@gmail.com');
$gapi->api_query({
method => 'post',
route => 'https://www.googleapis.com/calendar/v3/calendars/'.$calendar_id.'/events',
user => 'someuser@gmail.com'
}, $json_payload_if_post);
=head1 CONFIGURATION
config.json must be structured like:
{ "gapi":
{
"client_id": "001122334455-abcdefghijklmnopqrstuvwxyz012345.apps.googleusercontent.com",
"client_secret": "1ayL76NlEKjj85eZOipFZkyM",
"tokens": {
lib/API/Google.pm view on Meta::CPAN
=head2 build_headers
Keep access_token in headers always actual
$gapi->build_http_transactio($user);
=head2 build_http_transaction
$gapi->build_http_transaction({
user => 'someuser@gmail.com',
method => 'post',
route => 'https://www.googleapis.com/calendar/users/me/calendarList',
payload => { key => value }
})
=head2 api_query
Low-level method that can make API query to any Google service
Required params: method, route, user
Examples of usage:
$gapi->api_query({
method => 'get',
route => 'https://www.googleapis.com/calendar/users/me/calendarList'',
user => 'someuser@gmail.com'
});
$gapi->api_query({
method => 'post',
route => 'https://www.googleapis.com/calendar/v3/calendars/'.$calendar_id.'/events',
user => 'someuser@gmail.com'
}, $json_payload_if_post);
=head1 AUTHOR
Pavel Serikov <pavelsr@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by Pavel Serikov.
This is free software; you can redistribute it and/or modify it under
lib/API/Google/GCal.pm view on Meta::CPAN
my @n = grep { $_->{'summary'} eq $name } @$all;
my $full_id = $n[0]->{id};
return $full_id;
}
sub add_event {
my ($self, $user, $calendar_id, $event_data) = @_;
$self->api_query({
method => 'post',
route => $self->{api_base}.'/calendars/'.$calendar_id.'/events',
user => $user
}, $event_data);
}
sub busy_time_ranges {
my ($self, $params) = @_;
$self->api_query({
method => 'post',
route => $self->{api_base}.'/freeBusy',
user => $params->{user}
}, {
timeMin => $params->{dt_start},
timeMax => $params->{dt_end},
timeZone => $params->{timeZone},
items => [{ 'id' => $params->{calendarId} }]
});
};
lib/API/Google/Server.pm view on Meta::CPAN
helper get_new_tokens => sub {
my ($c,$auth_code) = @_;
my $hash = {};
$hash->{code} = $c->param('code');
$hash->{redirect_uri} = $c->url_for->to_abs->to_string;
$hash->{client_id} = $config->get('gapi/client_id');
$hash->{client_secret} = $config->get('gapi/client_secret');
$hash->{grant_type} = 'authorization_code';
my $tokens = $c->ua->post('https://www.googleapis.com/oauth2/v4/token' => form => $hash)->res->json;
return $tokens;
};
# =method get_all_google_jwk_keys
# Get all Google JWK keys for validation of JSON Web Token
# Check https://jwt.io/ and https://developers.google.com/identity/protocols/OpenIDConnect#validatinganidtoken for more details
# return arrayref
( run in 3.103 seconds using v1.01-cache-2.11-cpan-ceb78f64989 )