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