|
| 1 | +# Google Chat User Authorization App |
| 2 | + |
| 3 | +This sample demonstrates how to create a Google Chat app that requests |
| 4 | +authorization from the user to make calls to Chat API on their behalf. The first |
| 5 | +time the user interacts with the app, it requests offline OAuth tokens for the |
| 6 | +user and saves them to a Firestore database. If the user interacts with the app |
| 7 | +again, the saved tokens are used so the app can call Chat API on behalf of the |
| 8 | +user without asking for authorization again. Once saved, the OAuth tokens could |
| 9 | +even be used to call Chat API without the user being present. |
| 10 | + |
| 11 | +This app is built using Node.js on Google App Engine (Standard Environment) and |
| 12 | +leverages Google's OAuth2 for authorization and Firestore for data storage. |
| 13 | + |
| 14 | +**Key Features:** |
| 15 | + |
| 16 | +* **User Authorization:** Securely requests user consent to call Chat API with |
| 17 | + their credentials. |
| 18 | +* **Chat API Integration:** Calls Chat API to post messages on behalf of the |
| 19 | + user. |
| 20 | +* **Google Chat Integration:** Responds to DMs or @mentions in Google Chat. If |
| 21 | + necessary, request configuration to start an OAuth authorization flow. |
| 22 | +* **App Engine Deployment:** Provides step-by-step instructions for deploying |
| 23 | + to App Engine. |
| 24 | +* **Cloud Firestore:** Stores user tokens in a Firestore database. |
| 25 | + |
| 26 | +## Prerequisites |
| 27 | + |
| 28 | +* **Node.js:** [Download](https://www.nodejs.org/) |
| 29 | +* **Google Cloud SDK:** [Install](https://cloud.google.com/sdk/docs/install) |
| 30 | +* **Google Cloud Project:** [Create](https://console.cloud.google.com/projectcreate) |
| 31 | + |
| 32 | +## Deployment Steps |
| 33 | + |
| 34 | +1. **Enable APIs:** |
| 35 | + |
| 36 | + * Enable the Cloud Firestore and Google Chat APIs using the |
| 37 | + [console](https://console.cloud.google.com/apis/enableflow?apiid=firestore.googleapis.com,chat.googleapis.com) |
| 38 | + or gcloud: |
| 39 | + |
| 40 | + ```bash |
| 41 | + gcloud services enable firestore.googleapis.com chat.googleapis.com |
| 42 | + ``` |
| 43 | + |
| 44 | +1. **Initiate Deployment to App Engine:** |
| 45 | + |
| 46 | + * Go to [App Engine](https://console.cloud.google.com/appengine) and |
| 47 | + initialize an application. |
| 48 | + |
| 49 | + * Deploy the User Authorization app to App Engine: |
| 50 | + |
| 51 | + ```bash |
| 52 | + gcloud app deploy |
| 53 | + ``` |
| 54 | + |
| 55 | +1. **Create and Use OAuth Client ID:** |
| 56 | + |
| 57 | + * Get the app hostname: |
| 58 | + |
| 59 | + ```bash |
| 60 | + gcloud app describe | grep defaultHostname |
| 61 | + ``` |
| 62 | + |
| 63 | + * In your Google Cloud project, go to |
| 64 | + [APIs & Services > Credentials](https://console.cloud.google.com/apis/credentials). |
| 65 | + * Click `Create Credentials > OAuth client ID`. |
| 66 | + * Select `Web application` as the application type. |
| 67 | + * Add `<hostname from the previous step>/oauth2` to `Authorized redirect URIs`. |
| 68 | + * Download the JSON file and rename it to `client_secrets.json` in your |
| 69 | + project directory. |
| 70 | + * Redeploy the app with the file `client_secrets.json`: |
| 71 | + |
| 72 | + ```bash |
| 73 | + gcloud app deploy |
| 74 | + ``` |
| 75 | + |
| 76 | +1. **Create a Firestore Database:** |
| 77 | + |
| 78 | + * Create a Firestore database in native mode named `auth-data` using the |
| 79 | + [console](https://console.cloud.google.com/firestore) or gcloud: |
| 80 | + |
| 81 | + ```bash |
| 82 | + gcloud firestore databases create \ |
| 83 | + --database=auth-data \ |
| 84 | + --location=REGION \ |
| 85 | + --type=firestore-native |
| 86 | + ``` |
| 87 | + |
| 88 | + Replace `REGION` with a |
| 89 | + [Firestore location](https://cloud.google.com/firestore/docs/locations#types) |
| 90 | + such as `nam5` or `eur3`. |
| 91 | + |
| 92 | +## Create the Google Chat app |
| 93 | + |
| 94 | +* Go to |
| 95 | + [Google Chat API](https://console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat) |
| 96 | + and click `Configuration`. |
| 97 | +* In **App name**, enter `User Auth App`. |
| 98 | +* In **Avatar URL**, enter `https://developers.google.com/chat/images/quickstart-app-avatar.png`. |
| 99 | +* In **Description**, enter `Quickstart app`. |
| 100 | +* Under Functionality, select **Receive 1:1 messages** and |
| 101 | + **Join spaces and group conversations**. |
| 102 | +* Under **Connection settings**, select **HTTP endpoint URL** and enter your App |
| 103 | + Engine app's URL (obtained in the previous deployment steps). |
| 104 | +* In **Authentication Audience**, select **HTTP endpoint URL**. |
| 105 | +* Under **Visibility**, select **Make this Google Chat app available to specific |
| 106 | + people and groups in your domain** and enter your email address. |
| 107 | +* Click **Save**. |
| 108 | +
|
| 109 | +The Chat app is ready to receive and respond to messages on Chat. |
| 110 | +
|
| 111 | +## Interact with the App |
| 112 | +
|
| 113 | +* Add the app to a Google Chat space. |
| 114 | +* @mention the app. |
| 115 | +* Follow the authorization link to grant the app access to your account. |
| 116 | +* Once authorization is complete, the app will post a message to the space using |
| 117 | + your credentials. |
| 118 | +* If you @mention the app again, it will post a new message to the space with |
| 119 | + your credentials using the saved tokens, without asking for authorization again. |
| 120 | +
|
| 121 | +## Related Topics |
| 122 | +
|
| 123 | +* [Authenticate and authorize as a Google Chat user](https://developers.google.com/workspace/chat/authenticate-authorize-chat-user) |
| 124 | +* [Receive and respond to user interactions](https://developers.google.com/workspace/chat/receive-respond-interactions) |
0 commit comments