Skip to content

Commit c8fb5cf

Browse files
PierrickVouletpierrick
andauthored
feat: consolidate auth-app code sample in Python (#341)
Co-authored-by: pierrick <pierrick@google.com>
1 parent 9104018 commit c8fb5cf

File tree

7 files changed

+151
-372
lines changed

7 files changed

+151
-372
lines changed

python/auth-app/.gcloudignore

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
# $ gcloud topic gcloudignore
88
#
99
.gcloudignore
10-
1110
# If you would like to upload your .git directory, .gitignore file or files
1211
# from your .gitignore file, remove the corresponding line
1312
# below:
@@ -16,12 +15,5 @@
1615

1716
# Python pycache:
1817
__pycache__/
19-
2018
# Ignored by the build system
2119
/setup.cfg
22-
23-
# VSCode temporary files
24-
.history/
25-
26-
# Python virtual envs
27-
python3.10/

python/auth-app/README.md

Lines changed: 33 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,54 @@ This sample demonstrates how to create a Google Chat app that requests authoriza
1414
* **Python 3.7 or higher:** [Download](https://www.python.org/downloads/)
1515
* **Google Cloud SDK:** [Install](https://cloud.google.com/sdk/docs/install)
1616
* **Google Cloud Project:** [Create](https://console.cloud.google.com/projectcreate)
17-
* **Basic familiarity with Google Cloud Console and command line**
1817

1918
## Deployment Steps
2019

2120
1. **Enable APIs:**
22-
* Enable the Cloud Datastore API: [Enable Datastore API](https://console.cloud.google.com/flows/enableapi?apiid=datastore.googleapis.com)
23-
* Enable the People API: [Enable People API](https://console.cloud.google.com/flows/enableapi?apiid=people.googleapis.com)
24-
* Enable the Google Chat API: [Enable Chat API](https://console.cloud.google.com/flows/enableapi?apiid=chat.googleapis.com)
2521

26-
```bash
27-
gcloud services enable datastore.googleapis.com people.googleapis.com chat.googleapis.com
28-
```
22+
* Enable the Cloud Datastore, People, and Google Chat APIs using the
23+
[console](https://console.cloud.google.com/apis/enableflow?apiid=datastore.googleapis.com,people.googleapis.com,chat.googleapis.com)
24+
or gcloud:
2925

30-
2. **Create OAuth Client ID:**
31-
* In your Google Cloud project, go to [APIs & Services > Credentials](https://console.cloud.google.com/apis/credentials).
32-
* Click `Create Credentials > OAuth client ID`.
33-
* Select `Web application` as the application type.
34-
* Add `http://localhost:8080/auth/callback` to `Authorized redirect URIs` for local testing.
35-
* Download the JSON file and rename it to `client_secrets.json` in your project directory.
26+
```bash
27+
gcloud services enable datastore.googleapis.com people.googleapis.com chat.googleapis.com
28+
```
29+
30+
1. **Initiate Deployment to App Engine:**
31+
32+
* Open `app.yaml` and replace `<SERVICE_ACCOUNT>` with the email address of your App Engine
33+
default service account (you can find this in the
34+
[App Engine settings](https://console.cloud.google.com/appengine/settings) in Cloud Console).
3635

37-
3. **Deploy to App Engine:**
38-
* Open `app.yaml` and replace `<SERVICE_ACCOUNT>` with the email address of your App Engine default service account (you can find this in the [App Engine settings](https://console.cloud.google.com/appengine/settings) in Cloud Console).
3936
* Deploy the app:
37+
4038
```bash
4139
gcloud app deploy
4240
```
41+
42+
1. **Create and Use OAuth Client ID:**
43+
4344
* Get the app hostname:
45+
4446
```bash
4547
gcloud app describe | grep defaultHostname
4648
```
47-
* Update `client_secrets.json`: Replace `http://localhost:8080/auth/callback` in `Authorized redirect URIs` with `<hostname from the previous step>/auth/callback`.
48-
* Redeploy the app:
49+
50+
* In your Google Cloud project, go to [APIs & Services > Credentials](https://console.cloud.google.com/apis/credentials).
51+
* Click `Create Credentials > OAuth client ID`.
52+
* Select `Web application` as the application type.
53+
* Add `<hostname from the previous step>/auth/callback` to `Authorized redirect URIs`.
54+
* Download the JSON file and rename it to `client_secrets.json` in your project directory.
55+
* Redeploy the app with the file `client_secrets.json`:
56+
4957
```bash
5058
gcloud app deploy
5159
```
5260

53-
4. **Grant Datastore Permissions:**
61+
1. **Grant Datastore Permissions:**
62+
5463
* Grant the App Engine default service account permissions to access Datastore:
64+
5565
```bash
5666
PROJECT_ID=$(gcloud config list --format='value(core.project)')
5767
SERVICE_ACCOUNT_EMAIL=$(gcloud app describe | grep serviceAccount | cut -d ':' -f 2)
@@ -62,8 +72,11 @@ This sample demonstrates how to create a Google Chat app that requests authoriza
6272

6373
## Create the Google Chat app
6474

65-
* Go to [Google Chat API](https://developers.google.com/chat/api/guides/quickstart/apps-script) and click `Configuration`.
66-
* Enter your App Engine app's URL (obtained in the previous deployment steps) as the **HTTP endpoint URL**.
75+
* Go to
76+
[Google Chat API](https://developers.google.com/chat/api/guides/quickstart/apps-script)
77+
and click `Configuration`.
78+
* Enter your App Engine app's URL (obtained in the previous deployment steps)
79+
as the **HTTP endpoint URL**.
6780
* Complete the rest of the configuration as needed.
6881
6982
## Interact with the App
@@ -73,57 +86,3 @@ This sample demonstrates how to create a Google Chat app that requests authoriza
7386
* Follow the authorization link to grant the app access to your profile.
7487
* Send messages to the app to see your profile information.
7588
* Type `logout` to deauthorize the app.
76-
77-
## Run Locally
78-
79-
1. **Set up Service Account:**
80-
* Create a service account with the `Project > Editor` role.
81-
* Download the service account key as a JSON file (`service-account.json`).
82-
83-
2. **Set Environment Variable:**
84-
```bash
85-
export GOOGLE_APPLICATION_CREDENTIALS=./service-account.json
86-
```
87-
88-
3. **Create Virtual Environment (Recommended):**
89-
90-
```bash
91-
python3 -m venv env
92-
source env/bin/activate
93-
```
94-
95-
4. **Install Dependencies:**
96-
97-
```bash
98-
pip install -r requirements.txt
99-
```
100-
101-
5. **Run the App:**
102-
103-
```bash
104-
python main.py
105-
```
106-
107-
6. **Test the App:**
108-
109-
```bash
110-
curl \
111-
-H 'Content-Type: application/json' \
112-
--data '{
113-
"type": "MESSAGE",
114-
"configCompleteRedirectUrl": "https://www.example.com",
115-
"message": {
116-
"text": "header keyvalue",
117-
"thread": null
118-
},
119-
"user": {
120-
"name": "users/123",
121-
"displayName": "me"
122-
},
123-
"space": {
124-
"displayName": "space",
125-
"name": "spaces/-oMssgAAAAE"
126-
}
127-
}' \
128-
http://127.0.0.1:8080/
129-
```

python/auth-app/app.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,12 +14,14 @@
1414

1515
# This file specifies your Python application's runtime configuration.
1616
# See https://cloud.google.com/appengine/docs/managed-vms/python/runtime
17-
#
1817

19-
runtime: python310
18+
runtime: python312
2019

2120
env_variables:
22-
CLIENT_SECRET_PATH: "client_secret.json"
21+
# A JSON formatted file containing the client ID, client secret, and other OAuth 2.0 parameters
22+
CLIENT_SECRETS_PATH: "client_secrets.json"
23+
# Arbitrary secret key used by the Flask app to cryptographically sign session cookies
2324
SESSION_SECRET: "notasecret"
2425

26+
# The email address of the App Engine default service account
2527
service_account: <SERVICE_ACCOUNT>

0 commit comments

Comments
 (0)