The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Google::RestApi - Oauth2 connection to Google APIs (currently Drive and Sheets).

SYNOPSIS

      use Google::RestApi;
      $rest_api = Google::RestApi->new(
        login => {
          client_id     => <oauth2_client_id>,
          client_secret => <oath2_secret>,
        },
        token => <token_file_path>,
      );
    
      $response = $rest_api->api(
          uri     => <google_api_url>,
          method  => get|head|put|patch|post|delete,
          headers => [],
          params  => <query_params>,
          content => <data_for_body>,
      );
    
      use Google::RestApi::SheetsApi4;
      $sheets_api = Google::RestApi::SheetsApi4->new(api => $rest_api);
      $sheet = $sheets_api->open_spreadsheet(title => "payroll");
    
      use Google::RestApi::DriveApi3;
      $drive = Google::RestApi::DriveApi3->new(api => $rest_api);
      $file = $drive->file(id => 'xxxx');
      $copy = $file->copy(title => 'my-copy-of-xxx');
    
      print YAML::Any::Dump($rest_api->stats());

DESCRIPTION

Google Rest API is the foundation class used by the included Drive and Sheets APIs. It is used to establish an Oauth2 handshake, and send API requests to the Google API endpoint on behalf of the underlying API classes (Sheets and Drive).

Once you have established the Oauth2 handshake, you would not use this class much, it would be used indirectly by the Drive/Sheets API classes.

SUBROUTINES

new(login => <hash>, token => <token_file_path>);
 login:
   client_id: The Oauth2 client id you got from Google.
   client_secret: The Oauth2 client secret you got from Google.
 token: The file path to the previously saved token (see OAUTH2
   SETUP below).
api(uri => <uri_string>, method => <http_method_string>, headers => <headers_string_array>, params => <query_parameters_hash>, content => <body_hash>);

The ultimate Google API call for the underlying classes. Handles timeouts and retries etc. You would not normally call this directly, unless you need to for some special purpose.=

 uri: The Google API endpoint such as https://www.googleapis.com/drive/v3
   along with any path segments added.
 method: The http method being used get|head|put|patch|post|delete.
 headers: Array ref of http headers.
 params: Http query params to be added to the uri.
 content: The body being sent for post/put etc. Will be encoded to JSON.
stats();

Shows some statistics on how many get/put/post etc calls were made. Useful for performance tuning during development.

OAUTH2 SETUP

This class depends on first creating an Oauth2 token session file that you point to via the 'token' config param passed via 'new'. See bin/session_creator and follow the instructions to save your token file.

SEE ALSO

For specific use of this class, see:

 Google::RestApi::SheetsApi4
 Google::RestApi::DriveApi3

AUTHORS

  • Robin Murray mvsjes@cpan.org

COPYRIGHT

Copyright (c) 2019, Robin Murray. All rights reserved.

This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.