nysus has asked for the wisdom of the Perl Monks concerning the following question:

I have successfully set up access to Google's API via Moo::Google. Thanks to my fellow monks for telling me about this module. I was able to get it successfully working with the Calendar API which has example code in the documentation. I'm struggling, however, to figure out the method calls I need to make API calls to Google's People API in order to get results returned. When I do a call with $gapi->people (with lowercase 'p') the program hangs. Google's documentation appears to indicate a lowercase 'p' is needed but I'm not sure. When I try with an uppercase "P", I can't get any methods to work. I just get Can't locate object method "<name_of_method_tried>" via package "Moo::Google::People" at ./moo_google.pl line 12. no matter what methods I try to call. I have enabled the People API in the developer console for this app.

So how do I make calls to Google's People API with Perl?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Getting Moo::Google working with People API
by nysus (Parson) on Aug 04, 2018 at 16:54 UTC

    Solved with a lower level method, api_query:

    #! /usr/bin/env perl use Moo::Google; use Data::Dumper qw (Dumper); my $gapi = Moo::Google->new(debug => 0); my $user = 'me@gmail.com'; $gapi->auth_storage->setup({ type => 'jsonfile', path => 'config.json' + }); $gapi->user($user); $gapi->do_autorefresh; $res = $gapi->api_query( { httpMethod => 'get', path => 'https://people.googleapis.com/v1/people/me', options => { personFields => 'emailAddresses' } }); print Dumper $res;

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Re: Getting Moo::Google working with People API
by nysus (Parson) on Aug 03, 2018 at 17:41 UTC

    I've made a little headway with this:

    #! /usr/bin/env perl use Moo::Google; use Log::Log4perl::Shortcuts qw(:all); use Data::Dumper qw (Dumper); my $gapi = Moo::Google->new(debug => 0); my $user = me@gmail.com'; $gapi->auth_storage->setup({ type => 'jsonfile', path => 'config.json' + }); $gapi->user($user); $gapi->do_autorefresh; my $r1 = $gapi->People->People;

    Now I'm trying to figure out how to use the get method on $r1 to pull up my own information per the documentation.

    I've tried $r1->get({ resourceName => 'people/me' })->json; but the resource is not found. I also tried with $r1->People->get({'resourceName'  => 'people/me', 'personFields' => 'emailAddresses'})->json; but still no dice.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Re: Getting Moo::Google working with People API
by nysus (Parson) on Aug 04, 2018 at 13:54 UTC

    So, I've been able to make some more headway on this by delving into the code of Moo::Google::Client.pm. There is a method in that module called build_http_transaction. The method passes a $path scalar to a build_tx subroutine in mojolicious. The $path scalar is set by inspecting Google's discovery api and has the value of https://people.googleapis.com/v1/{+resourceName} for the API call here. If I hard code the path to:

    https://people.googleapis.com/v1/people/me and make the API call through Moo::Google with $r1->People->get({options => {personFields => 'emailAddresses'} )->json; I can actually get it to work and return results.

    So now the question is, how do I make the call via Moo::Google so that {+resourceName} in the path gets replaced with people/me? Maybe this is impossible with Moo::Google?

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      NB - Steve Dondleyhas copied Moo-Google into the cpan WebService::Google namespace. It should be noted that the code as of writing this is not yet ready for prime time but Steve is working on it so I am hopeful that it becomes usable soon.

      If looking to use this as a generic Google API interface be warned that although the agent works well for handling to OAUTH tokens, the dynamic classing isn't really usable except for the Calendar API. There is a lower level method that can be used to construct the endpoints and do REST directly which is really the only way you should expect to use this at the moment. The idea of autoclass generation for the API's based on the Google API discovery endpoints is a kinda cool ambition but it seems to break down quickly with the current implementation as only the top level methods can be used.

      An alternative approach for those seeking to make extended use of the Google API's might be to use the swagger specs as per https://github.com/APIs-guru/openapi-directory/tree/master/APIs/googleapis.com with something like Mojolicious::Plugin::OpenAPI as described at https://mojolicious.io/blog/2017/12/22/day-22-how-to-build-a-public-rest-api/

      If anybody goes down that path I'd love to here on the level of success. For my use cases I'm using the low level methods in my own version of WebService::Google::Client for GMail, GMB, Sheets etc so if anybody needs a hand on this front feel to reach out.

Re: Getting Moo::Google working with People API
by localshop (Monk) on Oct 02, 2018 at 17:43 UTC
    This module is a bit of a work in progress and may not work as expected but is getting some love to build on the work of the author Pavel Serikov. For now the best usage other than the Calendar API is to use the direct API call with manually constructed URI's with parameters as suggested in the other posts. The package includes a utility goauth for generating OAUTH tokens which is useful but be aware that the current cpan version has hard coded scopes. There is the start of some cool use of Google Discovery API to autogenerate access classes but this doesn't quite work for nested methods. If you are going to use this module I suggest that you look at the forks of the original Github repo at https://github.com/pavelsr/moo-google