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 API.

Prerequisites

To run this quickstart, you'll need:

  • Ruby 2.0 or greater.
  • A Google account with Google Drive enabled.

Step 1: Turn on the Google Apps Script API

Click this button to create a new console project and automatically enable the Google Apps Script API:

Enable the Google Apps Script API

This opens a new dialog. In the dialog, do the following:

  1. Select + Create a new project.
  2. Download the configuration file.
  3. Move the downloaded file to your working directory and ensure it is named credentials.json.

Step 2: Install the Google Client Library

Run the following command to install the library:

gem install google-api-client -v '~> 0.8'

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:

apps_script/quickstart/quickstart.rb
require 'google/apis/script_v1'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'fileutils'

OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
APPLICATION_NAME = 'Google Apps Script API Ruby Quickstart'.freeze
CREDENTIALS_PATH = 'credentials.json'.freeze
TOKEN_PATH = 'token.yaml'.freeze
SCOPE = 'https://www.googleapis.com/auth/script.projects'.freeze

##
# 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(CREDENTIALS_PATH)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: TOKEN_PATH)
  authorizer = Google::Auth::UserAuthorizer.new(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:\n" + url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI
    )
  end
  credentials
end

# Initialize the API
service = Google::Apis::ScriptV1::ScriptService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize

begin
  # Initialize the API
  service = Google::Apis::ScriptV1::ScriptService.new
  service.client_options.application_name = APPLICATION_NAME
  service.authorization = authorize

  # Make the API request.
  request = Google::Apis::ScriptV1::CreateProjectRequest.new(
    title: 'My Script'
  )
  resp = service.create_project(request)

  script_id = resp.script_id
  content = Google::Apis::ScriptV1::Content.new(
    files: [
      Google::Apis::ScriptV1::File.new(
        name: 'hello',
        type: 'SERVER_JS',
        source: "function helloWorld() {\n  console.log('Hello, world!');\n}"
      ),
      Google::Apis::ScriptV1::File.new(
        name: 'appsscript',
        type: 'JSON',
        source: "{\"timeZone\":\"America/New_York\",\"exceptionLogging\": \
          \"CLOUD\"}"
      )
    ],
    script_id: script_id
  )
  service.update_project_content(script_id, content)
  puts "https://script.google.com/d/#{script_id}/edit"
rescue Google::Apis::ServerError
  puts 'An error occurred on the server and the request can be retried.'
rescue Google::Apis::ClientError
  puts 'The request is invalid and should not be retried without modification.'
rescue Google::Apis::AuthorizationError
  puts 'Authorization is required.'
end

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.

Notes

  • 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

Send feedback about...

Apps Script API
Apps Script API
Need help? Visit our support page.