|
| 1 | +# Google Chat Connectivity App |
| 2 | + |
| 3 | +This sample demonstrates how to create a Google Chat app that requests |
| 4 | +authorization from the user to make API calls on their behalf. The first |
| 5 | +time the user interacts with the app, it requests offline OAuth credentials for the |
| 6 | +user and saves them to a Firestore database. If the user interacts with the app |
| 7 | +again, the saved credentials are used so the app can make API calls on behalf of the |
| 8 | +user without asking for authorization again. Once saved, the OAuth credentials could |
| 9 | +even be used without any further user interactions. |
| 10 | + |
| 11 | +This app is built using Java 21 and |
| 12 | +[Spring Boot](https://spring.io/projects/spring-boot) on Google App Engine |
| 13 | +(Standard Environment) and leverages Google's OAuth2 for authorization and |
| 14 | +Firestore for data storage. It replies with a Chat message that contains the |
| 15 | +link to a Meet space that was created calling the Google Meet API with their |
| 16 | +consent. |
| 17 | + |
| 18 | +**Key Features:** |
| 19 | + |
| 20 | +* **User Authorization:** Securely requests user consent to call Meet API with |
| 21 | + their credentials. |
| 22 | +* **Meet API Integration:** Calls Meet API to create a new Meet space on behalf |
| 23 | + of the user. |
| 24 | +* **Google Chat Integration:** Responds to DMs and @mentions in Google Chat. If |
| 25 | + necessary, request configuration to start an OAuth authorization flow. |
| 26 | +* **App Engine Deployment:** Provides step-by-step instructions for deploying |
| 27 | + to App Engine. |
| 28 | +* **Cloud Firestore:** Stores user credentials in a Firestore database. |
| 29 | + |
| 30 | +## Prerequisites |
| 31 | + |
| 32 | +* **JDK 21:** [Download](https://openjdk.org/projects/jdk/21/) |
| 33 | +* **Google Cloud SDK:** [Install](https://cloud.google.com/sdk/docs/install) |
| 34 | +* **Google Cloud Project:** [Create](https://console.cloud.google.com/projectcreate) |
| 35 | + |
| 36 | +## Deployment Steps |
| 37 | + |
| 38 | +1. **Set up your development environment:** |
| 39 | + |
| 40 | + * Follow the steps in |
| 41 | + [Setting Up Your Development Environment](https://cloud.google.com/appengine/docs/standard/setting-up-environment?tab=java) |
| 42 | + to install Java and the Google Cloud SDK. |
| 43 | + |
| 44 | + * Follow the steps in |
| 45 | + [Using Maven and the App Engine Plugin](https://cloud.google.com/appengine/docs/standard/java-gen2/using-maven) |
| 46 | + to install Maven. |
| 47 | + |
| 48 | +1. **Enable APIs:** |
| 49 | + |
| 50 | + * Enable the Cloud Firestore, Meet, and Google Chat APIs using the |
| 51 | + [console](https://console.cloud.google.com/apis/enableflow?apiid=firestore.googleapis.com,meet.googleapis.com,chat.googleapis.com) |
| 52 | + or gcloud: |
| 53 | + |
| 54 | + ```bash |
| 55 | + gcloud services enable firestore.googleapis.com meet.googleapis.com chat.googleapis.com |
| 56 | + ``` |
| 57 | + |
| 58 | +1. **Initiate Deployment to App Engine:** |
| 59 | + |
| 60 | + * Go to [App Engine](https://console.cloud.google.com/appengine) and |
| 61 | + initialize an application. |
| 62 | + |
| 63 | + * Deploy the app to App Engine: |
| 64 | + |
| 65 | + ```bash |
| 66 | + mvn clean package appengine:deploy -Dapp.deploy.projectId=YOUR_PROJECT_ID |
| 67 | + ``` |
| 68 | + |
| 69 | + Replace `YOUR_PROJECT_ID` with your Google Cloud Project ID. |
| 70 | + |
| 71 | +1. **Create and Use OAuth Client ID:** |
| 72 | + |
| 73 | + * Get the app hostname: |
| 74 | + |
| 75 | + ```bash |
| 76 | + gcloud app describe | grep defaultHostname |
| 77 | + ``` |
| 78 | + |
| 79 | + * In your Google Cloud project, go to |
| 80 | + [APIs & Services > Credentials](https://console.cloud.google.com/apis/credentials). |
| 81 | + * Click `Create Credentials > OAuth client ID`. |
| 82 | + * Select `Web application` as the application type. |
| 83 | + * Add `<hostname from the previous step>/oauth2` to `Authorized redirect URIs`. |
| 84 | + * Download the JSON file and rename it to `client_secrets.json` in the |
| 85 | + `src/main/resources/` subdirectory of your project directory. |
| 86 | + * Redeploy the app with the file `client_secrets.json`: |
| 87 | + |
| 88 | + ```bash |
| 89 | + mvn clean package appengine:deploy -Dapp.deploy.projectId=YOUR_PROJECT_ID |
| 90 | + ``` |
| 91 | + |
| 92 | + Replace `YOUR_PROJECT_ID` with your Google Cloud Project ID. |
| 93 | + |
| 94 | +1. **Create a Firestore Database:** |
| 95 | + |
| 96 | + * Create a Firestore database in native mode named `auth-data` using the |
| 97 | + [console](https://console.cloud.google.com/firestore) or gcloud: |
| 98 | + |
| 99 | + ```bash |
| 100 | + gcloud firestore databases create \ |
| 101 | + --database=auth-data \ |
| 102 | + --location=REGION \ |
| 103 | + --type=firestore-native |
| 104 | + ``` |
| 105 | + |
| 106 | + Replace `REGION` with a |
| 107 | + [Firestore location](https://cloud.google.com/firestore/docs/locations#types) |
| 108 | + such as `nam5` or `eur3`. |
| 109 | + |
| 110 | +## Create the Google Chat app |
| 111 | + |
| 112 | +* Go to |
| 113 | + [Google Chat API](https://console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat) |
| 114 | + and click `Configuration`. |
| 115 | +* In **App name**, enter `Connectivity App`. |
| 116 | +* In **Avatar URL**, enter `https://developers.google.com/chat/images/quickstart-app-avatar.png`. |
| 117 | +* In **Description**, enter `Connectivity app`. |
| 118 | +* Under **Functionality**, select **Join spaces and group conversations**. |
| 119 | +* Under **Connection settings**, select **HTTP endpoint URL** |
| 120 | +* Under **Triggers**, select **Use a common HTTP endpoint URL for all triggers**. |
| 121 | +* In **HTTP endpoint URL** enter your App Engine app's URL (obtained in the previous |
| 122 | + deployment steps) without the trailing `/`. |
| 123 | +* Under **Commands**, click **Add a command**, and click **Done** after setting: |
| 124 | + * **Command Id** to `1`. |
| 125 | + * **Description** and **Quick command name** to `Logout`. |
| 126 | +* Under **Visibility**, select **Make this Google Chat app available to specific |
| 127 | + people and groups in your domain** and enter your email address. |
| 128 | +* Click **Save**. |
| 129 | +
|
| 130 | +## Interact with the App |
| 131 | +
|
| 132 | +* Message the app. |
| 133 | +* Follow the authorization link to grant the app access to your account. |
| 134 | +* Once authorization is complete, the app will reply with a link to the newly |
| 135 | + created Meet space. |
| 136 | +* Message the app again, it will reply without asking for authorization. |
| 137 | +* Execute the quick command `Logout`, it will deauthorizes the app. |
| 138 | +
|
| 139 | +## Related Topics |
| 140 | +
|
| 141 | +* [Authenticate and authorize Chat apps and Google Chat API requests](https://developers.google.com/workspace/chat/authenticate-authorize) |
| 142 | +* [Build Google Chat interfaces](https://developers.google.com/workspace/add-ons/chat/build) |
0 commit comments