Google::RestApi - Connection to Google REST APIs (currently Drive and Sheets).


      use Google::RestApi;
      $rest_api = Google::RestApi->new(
        config_file   => <path_to_config_file>,
        client_id     => <oauth2_client_id>,
        client_secret => <oath2_secret>,
        token_file    => <path_to_token_file>,
        timeout       => <int>,
        throttle      => <int>,
        post_process  => <coderef>,
      $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());


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.


new(config_file => <path_to_config_file>, client_id => <str>, client_secret => <str>, token_file => <path_to_token_file>, post_process => <coderef>, throttle => <int>);
 config_file: Optional YAML configuration file that can specify any
   or all of the following args:
 client_id: The OAuth2 client id you got from Google.
 client_secret: The OAuth2 client secret you got from Google.
 token_file: The file path to the previously saved token (see OAUTH2
   SETUP below). If a config_file is passed, the dirname of the config
   file is tried to find the token_file (same directory) if only the
   token file name is passed.
 post_process: A coderef to call after each API call.
 throttle: Used in development to sleep the number of seconds
   specified between API calls to avoid threshhold errors from Google.

You can specify any of the arguments in the optional YAML config file. Any passed in arguments will override what is in the config file.

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.

 uri: The Google API endpoint such as
   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.

You would not normally call this directly unless you were making a Google API call not currently supported by this API framework.


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


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


For specific use of this class, see:



  • Robin Murray


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.