NAME

WebService::Google::Client - Google API Services Client.

VERSION

version 0.06

SYNOPSIS

use WebService::Google::Client;
use Data::Dumper;

my $gapi = WebService::Google::Client->new(debug => 0); # my $gapi = WebService::Google::Client->new(access_token => '');
my $user = 'peter@pscott.com.au'; # full gmail or G-Suite email

$gapi->auth_storage->setup({type => 'jsonfile', path => '/path' }); # by default

$gapi->user($user);       ## allows to select user configuration by google auth'd email address
$gapi->do_autorefresh(1); ## refresh auth token with refresh token if auth session has expired

my $r1 = $gapi->Calendar->Events->list({ calendarId => 'primary' })->json;
carp scalar @{$r1->{items}};

print Dumper $gapi_agent->Gmail->Users->getProfile( { userId => 'me'  } )->json;

print $gapi_agent->discovery->chi->root_dir(); ## provides the directory used to hold cached API discovery specs by CHI

To create or modify authorization file with scope and user tokens in current folder run goauth CLI tool

TYPICAL USAGE ( WITHOUT AUTO-GENERATED CLASSES )

use feature 'say';
use WebService::Google::Client;

my $gapi = WebService::Google::Client->new(debug => 0);
$gapi->auth_storage->setup({ type => 'jsonfile', path => './gapi.json' }) || croak('Unable to ');
my $aref_token_emails = $gapi->auth_storage->storage->get_token_emails_from_storage;
my $user = $aref_token_emails->[0];
print "Running tests with default user email = $user\n";
$gapi->user($user);
$gapi->do_autorefresh;

my $cl =   $gapi->api_query({
    method => 'get',
    path       => "https://www.googleapis.com/gmail/v1/users/me/messages?q=newer_than:1d;to:$user", 
});

if ($cl->code eq '200') ## Mojo::Message::Response
{
    foreach my $msg ( @{ $cl->json->{messages} } )
    {
        say $cl->to_string;
    }

}

See unit test in xt folder for more examples and some use cases for specific APIS in the /examples folder

KEY FEATURES

Object-oriented calls by API->Resource->method schema. Like $gapi->Calendar->Events->lists ( Under Review )
Classes are generated dynamically using Moose::Meta::Class based on Google API Discovery Service ( Under review )
API Discovery with local caching using CHI File
OAUTH app credentials (client_id, client_secret, users access_token && refresh_token) storage stored in local gapi.json file
Automatic access_token refresh (if user has refresh_token) and saving refreshed token to storage
CLI tool (goauth) with lightweight http server to simplify config OAuth2 configuration, sccoping, authorization and obtaining access_ and refresh_ tokens

TODO:

DBI and MongoDB not currently implemented
Refactor tests
Memoise discovery data
Option to Configure Auth Storage on new instance creation

SEE ALSO

AUTHORS

  • Pavel Serikov <pavelsr@cpan.org>

  • Peter Scott <peter@localshop.com.au>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017-2018 by Pavel Serikov, Peter Scott and others.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.