Ruby Quickstart

Complete the steps described in the rest of this page, and in about five minutes you'll have a simple Ruby command-line application that makes requests to the Google Apps Script Execution API.


To run this quickstart, you'll need:

  • Ruby 2.0 or greater.
  • Access to the internet and a web browser.
  • A Google account with Google Drive enabled.
  • A target Apps Script to call with the API.

Step 1: Turn on the Google Apps Script Execution API

  1. Open your target Apps Script in the editor and select Resources > Cloud Platform project.
  2. In the dialog that opens, click on the blue link (that starts with your script's name) at the top to open the console project associated with your script.
  3. The left sidebar should say API Manager. If it does not, click the icon in the upper-left to open a side panel and select API Manager. Select Library in the left sidebar.
  4. In the search bar under the Google APIs tab, enter "Google Apps Script Execution API". Click the same name in the list that appears. In the new tab that opens, click Enable API.
  5. Click Credentials in the left sidebar.
  6. Select the Credentials tab, click the Create credentials button and select OAuth client ID.
  7. Select the application type Other, enter the name "Google Apps Script Execution API Quickstart", and click the Create button.
  8. Click OK to dismiss the resulting dialog.
  9. Click the file_download (Download JSON) button to the right of the client ID.
  10. Move this file to your working directory and rename it client_secret.json.

Step 2: Install the Google Client Library

Run the following command to install the library:

gem install google-api-client

See the library's installation page for the alternative installation options.

Step 3: Set up the sample

Create a file named quickstart.rb in your working directory and copy in the following code:

require 'google/apis/script_v1'
require 'googleauth'
require 'googleauth/stores/file_token_store'

require 'fileutils'

OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
APPLICATION_NAME = 'Google Apps Script Execution API Ruby Quickstart'
CLIENT_SECRETS_PATH = 'client_secret.json'
CREDENTIALS_PATH = File.join(Dir.home, '.credentials',
SCOPE = ''

# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
def authorize

  client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
  token_store = CREDENTIALS_PATH)
  authorizer =
    client_id, SCOPE, token_store)
  user_id = 'default'
  credentials = authorizer.get_credentials(user_id)
  if credentials.nil?
    url = authorizer.get_authorization_url(
      base_url: OOB_URI)
    puts "Open the following URL in the browser and enter the " +
         "resulting code after authorization"
    puts url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI)

# Initialize the API
service =
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize

# Create an execution request object.
request =
  function: 'getFoldersUnderRoot'

  # Make the API request.
  resp = service.run_script(SCRIPT_ID, request)

  if resp.error
    # The API executed, but the script returned an error.

    # Extract the first (and only) set of error details. The values of this
    # object are the script's 'errorMessage' and 'errorType', and an array of
    # stack trace elements.
    error = resp.error.details[0]

    puts "Script error message: #{error['errorMessage']}"

    if error['scriptStackTraceElements']
      # There may not be a stacktrace if the script didn't start executing.
      puts "Script error stacktrace:"
      error['scriptStackTraceElements'].each do |trace|
        puts "\t#{trace['function']}: #{trace['lineNumber']}"
    # The structure of the result will depend upon what the Apps Script function
    # returns. Here, the function returns an Apps Script Object with String keys
    # and values, and so the result is treated as a Ruby hash (folderSet).
    folder_set = resp.response['result']
    if folder_set.length == 0
      puts "No folders returned!"
      puts "Folders under your root folder:"
      folder_set.each do |id, folder|
        puts "\t#{folder} (#{id})"
rescue Google::Apis::ClientError
  # The API encountered a problem before the script started executing.
  puts "Error calling API!"

Be sure to also replace the placeholder ENTER_YOUR_SCRIPT_ID_HERE with the script ID of your target script.

Step 4: Run the sample

Run the sample using the following command:

ruby quickstart.rb

The first time you run the sample, it will prompt you to authorize access:

  1. The sample will attempt to open a new window or tab in your default browser. If this fails, copy the URL from the console and manually open it in your browser.

    If you are not already logged into your Google account, you will be prompted to log in. If you are logged into multiple Google accounts, you will be asked to select one account to use for the authorization.

  2. Click the Accept button.
  3. The sample will proceed automatically, and you may close the window/tab.


  • Authorization information is stored on the file system, so subsequent executions will not prompt for authorization.
  • The authorization flow in this example is designed for a command-line application. For information on how to perform authorization in a web application, see Using OAuth 2.0 for Web Server Applications.

Further reading


Apps Script
Apps Script