|
1 | | -# Google Chat authorization app |
2 | | - |
3 | | -This code sample creates a Google Chat app that requests additional |
4 | | -authorizations from the user. This app retrieves the user's Google profile |
5 | | -information from [People API](https://developers.google.com/people/), and |
6 | | -is performing authorization against |
7 | | -[Google's OAuth2](https://developers.google.com/identity/protocols/OAuth2WebServer) |
8 | | -endpoints. |
9 | | - |
10 | | -The sample is built using Python on Google App Engine, Standard Environment. |
11 | | - |
12 | | -For more information on connecting a Chat app with other services and tools, |
13 | | -please read the |
14 | | -[guide](https://developers.google.com/workspace/chat/connect-web-services-tools). |
15 | | - |
16 | | -## Deploy the sample |
17 | | - |
18 | | - 1. Follow the steps in [Setting Up Your Development Environment](https://cloud.google.com/appengine/docs/standard/python3/setting-up-environment) |
19 | | - to install Python and the Google Cloud SDK |
20 | | - 1. Follow the steps in [Setting Up Your GCP Resources](https://cloud.google.com/appengine/docs/standard/python3/console/#create) |
21 | | - to create a project and enable App Engine. |
22 | | - 1. Enable the People API for your project using |
23 | | - [this wizard](https://console.cloud.google.com/flows/enableapi?apiid=people.googleapis.com). |
24 | | - 1. Enable the Cloud Datastore API for your project using |
25 | | - [this wizard](https://console.cloud.google.com/flows/enableapi?apiid=datastore.googleapis.com). |
26 | | - 1. Follow [instructions](https://support.google.com/googleapi/answer/6158849?hl=en) for creating |
27 | | - an oauth client ID for your project. Use the type "Web application" and a redirect |
28 | | - URI of \ |
29 | | - `https://<project ID>.appspot.com/auth/callback`. |
30 | | - 1. Download the associated JSON file, move it to this directory, and name it |
31 | | - `client_secret.json`. |
32 | | - |
33 | | - 1. Run the following command to deploy the app: |
| 1 | +# Google Chat Authorization App |
| 2 | + |
| 3 | +This sample demonstrates how to create a Google Chat app that requests authorization from the user to access their Google profile information using the People API. This app is built using Python on Google App Engine (Standard Environment) and leverages Google's OAuth2 for authorization. |
| 4 | + |
| 5 | +**Key Features:** |
| 6 | + |
| 7 | +* **User Authorization:** Securely requests user consent to access their Google profile data. |
| 8 | +* **People API Integration:** Retrieves and displays user profile information. |
| 9 | +* **Google Chat Integration:** Responds to @mentions in Google Chat. |
| 10 | +* **App Engine Deployment:** Provides step-by-step instructions for deploying to App Engine. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +* **Python 3.7 or higher:** [Download](https://www.python.org/downloads/) |
| 15 | +* **Google Cloud SDK:** [Install](https://cloud.google.com/sdk/docs/install) |
| 16 | +* **Google Cloud Project:** [Create](https://console.cloud.google.com/projectcreate) |
| 17 | +* **Basic familiarity with Google Cloud Console and command line:** |
| 18 | + |
| 19 | +## Deployment Steps |
| 20 | + |
| 21 | +1. **Enable APIs:** |
| 22 | + * Enable the People API: [Enable People API](https://console.cloud.google.com/flows/enableapi?apiid=people.googleapis.com) |
| 23 | + * Enable the Cloud Datastore API: [Enable Datastore API](https://console.cloud.google.com/flows/enableapi?apiid=datastore.googleapis.com) |
| 24 | + |
| 25 | +2. **Create OAuth Client ID:** |
| 26 | + * In your Google Cloud project, go to [APIs & Services > Credentials](https://console.cloud.google.com/apis/credentials). |
| 27 | + * Click "Create Credentials" > "OAuth client ID". |
| 28 | + * Select "Web application" as the application type. |
| 29 | + * Add `http://localhost:8080/auth/callback` to "Authorized redirect URIs" for local testing. |
| 30 | + * Download the JSON file and rename it to `client_secrets.json` in your project directory. |
| 31 | + |
| 32 | +3. **Deploy to App Engine:** |
| 33 | + * 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 in Cloud Console). |
| 34 | + * Deploy the app: |
| 35 | + ```bash |
| 36 | + gcloud app deploy |
34 | 37 | ``` |
| 38 | + * Get the app hostname: |
| 39 | + ```bash |
| 40 | + gcloud app describe | grep defaultHostname |
| 41 | + ``` |
| 42 | + * Update `client_secrets.json`: Replace `http://localhost:8080/auth/callback` in "Authorized redirect URIs" with `<hostname from the previous step>/auth/callback`. |
| 43 | + * Redeploy the app: |
| 44 | + ```bash |
35 | 45 | gcloud app deploy |
36 | 46 | ``` |
37 | 47 |
|
38 | | -## Configure the app for Google Chat |
| 48 | +4. **Grant Datastore Permissions:** |
| 49 | + * Grant the App Engine default service account permissions to access Datastore: |
| 50 | + ```bash |
| 51 | + PROJECT_ID=$(gcloud config list --format='value(core.project)') |
| 52 | + SERVICE_ACCOUNT_EMAIL=$(gcloud app describe | grep serviceAccount | cut -d ':' -f 2) |
| 53 | + gcloud projects add-iam-policy-binding $PROJECT_ID \ |
| 54 | + --member="serviceAccount:$SERVICE_ACCOUNT_EMAIL" \ |
| 55 | + --role="roles/datastore.owner" |
| 56 | + ``` |
39 | 57 |
|
40 | | - 1. To configure the app to respond to @mentions in Google Chat, follow |
41 | | - the steps to enable the API in |
42 | | - [Publishing apps](https://developers.google.com/chat/how-tos/apps-publish). |
43 | | - 1. When configuring the app on the **Configuration** tab on the |
44 | | - **Google Chat API** page, enter the URL for the deployed version |
45 | | - of the app into the **Bot URL** text box. |
| 58 | +## Configure Google Chat Integration |
46 | 59 |
|
47 | | -## Interact with the app |
| 60 | +1. **Enable the Google Chat API:** [Enable Chat API](https://console.cloud.google.com/flows/enableapi?apiid=chat.googleapis.com) |
| 61 | +2. **Create a Google Chat App:** |
| 62 | + * Go to [Google Chat API](https://developers.google.com/chat/api/guides/quickstart/apps-script) and click "Configuration". |
| 63 | + * Enter your App Engine app's URL (obtained in the previous deployment steps) as the **Bot URL**. |
| 64 | + * Complete the rest of the configuration as needed. |
48 | 65 |
|
49 | | -Either add and @mention the app in a room or in a direct mention to engage with the app. |
| 66 | +## Interact with the App |
50 | 67 |
|
51 | | -When first messaged or added to a space, the app will respond with a private rqeuest |
52 | | -to configure the app. Follow the link to authorize access to your profile data. Subsequent |
53 | | -messages will display a card with your profile. |
| 68 | +* Add the app to a Google Chat space. |
| 69 | +* @mention the app. |
| 70 | +* Follow the authorization link to grant the app access to your profile. |
| 71 | +* Send messages to the app to see your profile information. |
| 72 | +* Type "logout" to deauthorize the app. |
54 | 73 |
|
55 | | -To deauthorize the app, message "logout" to the app. |
| 74 | +## Run Locally |
56 | 75 |
|
57 | | -## Run the sample locally |
| 76 | +1. **Set up Service Account:** |
| 77 | + * Create a service account with the "Project > Editor" role. |
| 78 | + * Download the service account key as a JSON file (`service-acct.json`). |
58 | 79 |
|
59 | | -Note: Follow the steps for deployment and configuring the app for Google Chat |
60 | | -before running locally. |
| 80 | +2. **Set Environment Variable:** |
| 81 | + ```bash |
| 82 | + export GOOGLE_APPLICATION_CREDENTIALS=./service-acct.json |
| 83 | +```` |
61 | 84 |
|
62 | | - 1. Create a service account for the app, as documented |
63 | | - [here](https://developers.google.com/chat/api/guides/auth/service-accounts). |
64 | | - Save the private key in a `service-acct.json` file in the working directory. |
65 | | - 1. Start a virtual environment |
66 | | - ``` |
67 | | - python3 -m venv python3.10 |
68 | | - source python3.10/bin/activate |
69 | | - ``` |
70 | | - 1. Install libraries using `pip`. |
71 | | - `pip install -r requirements.txt --upgrade` |
72 | | - 1. Run the sample. |
73 | | - `GOOGLE_APPLICATION_CREDENTIALS=service-acct.json python main.py` |
| 85 | +3. **Create Virtual Environment (Recommended):** |
74 | 86 |
|
75 | | -To verify that the sample is running and responds with the correct data |
76 | | -to incoming requests, run the following command from the terminal: |
| 87 | + ```bash |
| 88 | + python3 -m venv venv |
| 89 | + source venv/bin/activate |
| 90 | + ``` |
77 | 91 |
|
78 | | -``` |
79 | | -curl -H 'Content-Type: application/json' --data '{"type": "MESSAGE", "configCompleteRedirectUrl": "https://www.example.com", "message": { "text": "header keyvalue", "thread": null }, "user": { "name": "users/123", "displayName": "me"}, "space": { "displayName": "space", "name": "spaces/-oMssgAAAAE"}}' http://127.0.0.1:8080/ |
80 | | -``` |
| 92 | +4. **Install Dependencies:** |
81 | 93 |
|
82 | | -## Shut down the local environment |
| 94 | + ```bash |
| 95 | + pip install -r requirements.txt |
| 96 | + ``` |
83 | 97 |
|
84 | | -``` |
85 | | -deactivate |
86 | | -``` |
| 98 | +5. **Run the App:** |
87 | 99 |
|
88 | | -## Troubleshooting |
| 100 | + ```bash |
| 101 | + python main.py |
| 102 | + ``` |
89 | 103 |
|
90 | | -Note: When running this sample, you may receive an error about |
91 | | -SpooledTemporaryFile class missing from the werkzeug module. To fix this, after |
92 | | -you've downloaded all of the support libraries to lib/ open up |
93 | | -lib/werkzeug/formparser.py and change the following line |
| 104 | +6. **Test the App:** |
94 | 105 |
|
95 | 106 | ``` |
96 | | -from tempfile import SpooledTemporaryFile |
| 107 | +curl \ |
| 108 | + -H 'Content-Type: application/json' \ |
| 109 | + --data '{ |
| 110 | + "type": "MESSAGE", |
| 111 | + "configCompleteRedirectUrl": "https://www.example.com", |
| 112 | + "message": { |
| 113 | + "text": "header keyvalue", |
| 114 | + "thread": null |
| 115 | + }, |
| 116 | + "user": { |
| 117 | + "name": "users/123", |
| 118 | + "displayName": "me" |
| 119 | + }, |
| 120 | + "space": { |
| 121 | + "displayName": "space", |
| 122 | + "name": "spaces/-oMssgAAAAE" |
| 123 | + } |
| 124 | + }' \ |
| 125 | + http://127.0.0.1:8080/ |
97 | 126 | ``` |
98 | 127 |
|
99 | | -to |
| 128 | +## Troubleshooting |
100 | 129 |
|
101 | | -``` |
102 | | -from tempfile import TemporaryFile |
103 | | -``` |
| 130 | + * **`SpooledTemporaryFile` Error:** If you encounter an error related to the `SpooledTemporaryFile` class, replace `from tempfile import SpooledTemporaryFile` with `from tempfile import TemporaryFile` in `lib/werkzeug/formparser.py`. |
| 131 | + * **Other Errors:** Refer to the [Google Chat API documentation](https://www.google.com/url?sa=E&source=gmail&q=https://developers.google.com/chat/api/guides/overview) and [App Engine documentation](https://cloud.google.com/appengine/docs) for troubleshooting and common issues. |
0 commit comments