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
- A Business or Enterprise Google Workspace account with access to Google Chat.
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:
- In the Google Cloud console, go to the Select a project page.
- 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.
Configure the OAuth consent screen
- In the Google Cloud console, go to Menu > APIs & Services > OAuth consent screen.
- For User type select Internal, then click Create.
- Complete the app registration form, then click Save and Continue.
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.
- 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.
In the Google Cloud console, go to the Chat API Configuration page:
Under Application info, enter the following information:
- In the App name field, enter
Chat API quickstart app
. - In the Avatar URL field, enter
https://developers.google.com/chat/images/quickstart-app-avatar.png
. - In the Description field, enter
Quickstart for calling the Chat API
.
- In the App name field, enter
Under Interactive features, click the Enable interactive features toggle to the off position to disable interactive features for the Chat app.
Click Save.
Create the script
- Create a new script by going to script.google.com/create.
- Replace the contents of the script editor with the following code:
- Click Save .
- Click Untitled project, type Quickstart, and click Rename.
Configure the script
Copy the Cloud project number
- In the Google Cloud console, go to Menu > IAM & Admin > Settings.
- In the Project number field, copy the value.
Link your Google Cloud project
- Open the Apps Script project.
- Click Project Settings .
- Under Google Cloud Platform (GCP) Project, click Change project.
- In GCP project number, paste the Google Cloud project number.
- Click Set project.
Enable the Google Chat API
- Open the Apps Script project.
- Click Project Settings .
- Under General settings, enable Show "appsscript.json" manifest file in editor.
- Click Editor
appscript.json
, and replace the contents with the following code:
, select the
file
Run the sample
In the Apps Script editor, click Run.
The first time you run the sample, it prompts you to authorize access:
- Click Review permissions.
- Choose an account.
- Click Allow.
The script's execution log appears at the bottom of the window.