Skip to content

Commit 910bf04

Browse files
authored
feat: Add Java User Authorization App sample (#349)
1 parent 74a1ff7 commit 910bf04

File tree

12 files changed

+1014
-0
lines changed

12 files changed

+1014
-0
lines changed

java/user-auth-app/.gcloudignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file specifies files that are *not* uploaded to Google Cloud
2+
# using gcloud. It follows the same syntax as .gitignore, with the addition of
3+
# "#!include" directives (which insert the entries of the given .gitignore-style
4+
# file at that point).
5+
#
6+
# For more information, run:
7+
# $ gcloud topic gcloudignore
8+
#
9+
.gcloudignore
10+
README.md
11+
# If you would like to upload your .git directory, .gitignore file or files
12+
# from your .gitignore file, remove the corresponding line
13+
# below:
14+
.git
15+
.gitignore
16+
# Target directory for maven builds
17+
target/

java/user-auth-app/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**/client_secrets.json
2+
target/

java/user-auth-app/README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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)

java/user-auth-app/pom.xml

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2025 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
21+
<name>user-auth-app</name>
22+
23+
<groupId>com.google.chat</groupId>
24+
<artifactId>user-auth-app</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
27+
<properties>
28+
<maven.compiler.target>17</maven.compiler.target>
29+
<maven.compiler.source>17</maven.compiler.source>
30+
</properties>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
<version>3.2.3</version>
37+
<exclusions>
38+
<!-- Exclude the Tomcat dependency -->
39+
<exclusion>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-tomcat</artifactId>
42+
</exclusion>
43+
</exclusions>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-jetty</artifactId>
48+
<version>3.2.3</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.fasterxml.jackson.core</groupId>
52+
<artifactId>jackson-databind</artifactId>
53+
<version>2.17.2</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.google.guava</groupId>
57+
<artifactId>guava</artifactId>
58+
<version>33.4.0-jre</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.google.protobuf</groupId>
62+
<artifactId>protobuf-java</artifactId>
63+
<version>4.29.2</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.google.cloud</groupId>
67+
<artifactId>google-cloud-firestore</artifactId>
68+
<version>3.30.2</version>
69+
</dependency>
70+
71+
<!-- Google OAuth2 Client Library -->
72+
<dependency>
73+
<groupId>com.google.auth</groupId>
74+
<artifactId>google-auth-library-credentials</artifactId>
75+
<version>1.30.1</version>
76+
</dependency>
77+
<dependency>
78+
<groupId>com.google.auth</groupId>
79+
<artifactId>google-auth-library-oauth2-http</artifactId>
80+
<version>1.30.1</version>
81+
</dependency>
82+
<dependency>
83+
<groupId>com.google.oauth-client</groupId>
84+
<artifactId>google-oauth-client-jetty</artifactId>
85+
<version>1.37.0</version>
86+
</dependency>
87+
<dependency>
88+
<groupId>com.google.api-client</groupId>
89+
<artifactId>google-api-client</artifactId>
90+
<version>2.7.1</version>
91+
</dependency>
92+
93+
<!-- Google Cloud Chat library -->
94+
<!-- Used for instantiating Google Chat API protos -->
95+
<dependency>
96+
<groupId>com.google.api.grpc</groupId>
97+
<artifactId>proto-google-cloud-chat-v1</artifactId>
98+
<version>0.19.0</version>
99+
</dependency>
100+
<!-- Used for creating Chat Service Client for making Google Chat API calls -->
101+
<dependency>
102+
<groupId>com.google.cloud</groupId>
103+
<artifactId>google-cloud-chat</artifactId>
104+
<version>0.19.0</version>
105+
</dependency>
106+
<dependency>
107+
<groupId>com.google.api</groupId>
108+
<artifactId>gax-bom</artifactId>
109+
<version>2.59.0</version>
110+
<type>pom</type>
111+
</dependency>
112+
</dependencies>
113+
114+
<build>
115+
<plugins>
116+
<plugin>
117+
<groupId>org.springframework.boot</groupId>
118+
<artifactId>spring-boot-maven-plugin</artifactId>
119+
<version>3.2.3</version>
120+
<executions>
121+
<execution>
122+
<goals>
123+
<goal>repackage</goal>
124+
</goals>
125+
</execution>
126+
</executions>
127+
</plugin>
128+
129+
<plugin>
130+
<groupId>com.google.cloud.tools</groupId>
131+
<artifactId>appengine-maven-plugin</artifactId>
132+
<version>2.7.0</version>
133+
<configuration>
134+
<version>GCLOUD_CONFIG</version>
135+
</configuration>
136+
</plugin>
137+
</plugins>
138+
</build>
139+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
runtime: java17
16+
instance_class: F1
17+
18+
# Explicitly set the memory limit and maximum heap size for the Spring Boot app
19+
env_variables:
20+
JAVA_TOOL_OPTIONS: "-XX:MaxRAM=256m -XX:ActiveProcessorCount=2 -Xmx32m"

0 commit comments

Comments
 (0)