This sample demonstrates how to create a Google Chat app that requests authorization from the user to make API calls on their behalf. The first time the user interacts with the app, it requests offline OAuth credentials for the user and saves them to storage. If the user interacts with the app again, the saved credentials are used so the app can make API calls on behalf of the user without asking for authorization again. Once saved, the OAuth credentials could even be used without any further user interactions.
This app is built using Apps Script and leverages Google's OAuth2 for authorization and Apps Script's User Properties for data storage. It replies with a Chat message that contains the link to a Meet space that was created calling the Google Meet API with their consent.
Key Features:
- User Authorization: Securely requests user consent to call Meet API with their credentials.
- Meet API Integration: Calls Meet REST API to create a new Meet space on behalf of the user.
- Google Chat Integration: Responds to DMs and @mentions in Google Chat. If necessary, request configuration to start an OAuth authorization flow.
- Apps Script Deployment: Provides step-by-step instructions for deploying to Apps Script.
-
Enable APIs:
- Enable the Meet, and Google Chat APIs using the console.
-
Deploy Apps Script Project:
- Open the project from Apps Script console.
- In
Project Settings, enable the optionShow "appsscript.json" manifest file in editorand copy theScript ID. - In
Editor, replace the source filesappsscript.jsonandCode.gswith the ones found in this directory. - Click
DeploythenTest deploymentsfrom the top right corner. - In the opened dialog, click
Installand copy theHead Deployment ID.
-
Create and Use OAuth Client ID:
- In your Google Cloud project, go to APIs & Services > Credentials.
- Click
Create Credentials > OAuth client ID. - Select
Web applicationas the application type. - Add
https://script.google.com/macros/d/<Script ID from the previous step>/usercallbacktoAuthorized redirect URIs. - Download the JSON file and rename it to
client_secrets.json.
-
Configure Apps Script Project Auth:
- Go to back to the project from Apps Script console.
- In
Editor, open the source fileCode.gs. - Set the varibale value
CLIENT_SECRETSto the entire content of the fileclient_secrets.jsonfrom the previous step. - Save to automatically deploy the change.
- Go to
Google Chat API
and click
Configuration. - In App name, enter
Connectivity App. - In Avatar URL, enter
https://developers.google.com/chat/images/quickstart-app-avatar.png. - In Description, enter
Connectivity app. - Under Functionality, select Join spaces and group conversations.
- Under Connection settings, select Apps Script and enter the
Head Deployment ID(obtained in the previous deployment steps) in Deployment ID. - Under Commands, click Add a command, and click Done after setting:
- Command Id to
1. - Description and Quick command name to
Logout.
- Command Id to
- Under Visibility, select Make this Google Chat app available to specific people and groups in your domain and enter your email address.
- Click Save.
- Message the app.
- Follow the authorization link to grant the app access to your account.
- Once authorization is complete, the app will reply with a link to the newly created Meet space.
- Message the app again, it will reply without asking for authorization.
- Execute the quick command
Logout, it will deauthorizes the app.