Google Apps Script quickstart

Quickstarts explain how to set up and run an app that calls a Google Workspace API.

Google Workspace quickstarts use the API client libraries to handle some details of the authentication and authorization flow. We recommend that you use the client libraries for your own apps. This quickstart uses a simplified authentication approach that is appropriate for a testing environment. For a production environment, we recommend learning about authentication and authorization before choosing the access credentials that are appropriate for your app.

Create a Google Apps Script that makes requests to the Google Chat API.

Objectives

  • Configure the environment.
  • Create and configure the script.
  • Run the script.

Prerequisites

Configure your Cloud project

If you're using a new Google Cloud project to complete this quickstart, you need to configure it and add yourself as a test user. If you've already completed these steps for your Cloud project, skip to the next section.

Open your Cloud project in the Google Cloud console

If it's not open already, open the Cloud project that you intend to use for this sample:

  1. In the Google Cloud console, go to the Select a project page.

    Select a Cloud project

  2. Select the Google Cloud project you want to use. Or, click Create project and follow the on-screen instructions. If you create a Google Cloud project, you might need to turn on billing for the project.

Turn on the Chat API

Before using Google APIs, you need to turn them on in a Google Cloud project. You can turn on one or more APIs in a single Google Cloud project.
  • In the Google Cloud console, enable the Google Chat API.

    Enable the API

  1. In the Google Cloud console, go to Menu > APIs & Services > OAuth consent screen.

    Go to OAuth consent screen

  2. For User type select Internal, then click Create.
  3. Complete the app registration form, then click Save and Continue.
  4. For now, you can skip adding scopes and click Save and Continue. In the future, when you create an app for use outside of your Google Workspace organization, you must change the User type to External, and then, add the authorization scopes that your app requires.

  5. Review your app registration summary. To make changes, click Edit. If the app registration looks OK, click Back to Dashboard.

Configure the Google Chat app

To call the Google Chat API, you must configure a Google Chat app. For any write requests, Google Chat attributes the Google Chat app in the UI using the following information.

  1. In the Google Cloud console, go to the Chat API Configuration page:

    Go to Chat API Configuration page

  2. Under Application info, enter the following information:

    1. In the App name field, enter Chat API quickstart app.
    2. In the Avatar URL field, enter https://developers.google.com/chat/images/quickstart-app-avatar.png.
    3. In the Description field, enter Quickstart for calling the Chat API.
  3. Under Interactive features, click the Enable interactive features toggle to the off position to disable interactive features for the Chat app.

  4. Click Save.

Create the script

  1. Create a new script by going to script.google.com/create.
  2. Replace the contents of the script editor with the following code:

chat/quickstart/Code.gs
/**
 * This quickstart sample shows how to list spaces with user credential
 *
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces.readonly'
 * referenced in the manifest file (appsscript.json).
 */
function listSpaces() {
  // Initialize request argument(s)
  // Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)
  const filter = 'space_type = "SPACE"';

  // Iterate through the response pages using page tokens
  let responsePage;
  let pageToken = null;
  do {
    // Request response pages
    responsePage = Chat.Spaces.list({
      filter: filter,
      pageToken: pageToken
    });
    // Handle response pages
    if (responsePage.spaces) {
      responsePage.spaces.forEach((space) => console.log(space));
    }
    // Update the page token to the next one
    pageToken = responsePage.nextPageToken;
  } while (pageToken);
}

  1. Click Save .
  2. Click Untitled project, type Quickstart, and click Rename.

Configure the script

Copy the Cloud project number

  1. In the Google Cloud console, go to Menu > IAM & Admin > Settings.

    Go to IAM & Admin Settings

  2. In the Project number field, copy the value.
  1. Open the Apps Script project.
  2. Click Project Settings .
  3. Under Google Cloud Platform (GCP) Project, click Change project.
  4. In GCP project number, paste the Google Cloud project number.
  5. Click Set project.

Enable the Google Chat API

  1. Open the Apps Script project.
  1. Click Project Settings .
  2. Under General settings, enable Show "appsscript.json" manifest file in editor.
  3. Click Editor , select the file appscript.json, and replace the contents with the following code:

chat/quickstart/appsscript.json
{
  "timeZone": "America/New_York",
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": [
    "https://www.googleapis.com/auth/chat.spaces.readonly"
  ],
  "chat": {},
  "dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Chat",
      "version": "v1",
      "serviceId": "chat"
    }]
  }
}

Run the sample

In the Apps Script editor, click Run.

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

  1. Click Review permissions.
  2. Choose an account.
  3. Click Allow.

The script's execution log appears at the bottom of the window.

Next steps