Skip to content

Commit c9c3f28

Browse files
PierrickVouletpierrick
and
pierrick
authored
feat: Add Google Chat API quickstart (#1624)
Co-authored-by: pierrick <pierrick@google.com>
1 parent 08e95b1 commit c9c3f28

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

chat/quickstart/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Google Chat Python Quickstart
2+
3+
Complete the steps described in the [quickstart instructions](
4+
https://developers.google.com/workspace/chat/api/guides/quickstart/python),
5+
and in about five minutes you'll have a simple Python command-line
6+
application that makes requests to the Google Chat API.
7+
8+
## Install
9+
10+
`pip install -r requirements`
11+
12+
## Run
13+
14+
After following the quickstart setup instructions, run the sample:
15+
16+
`python3 quickstart.py`

chat/quickstart/quickstart.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2024 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+
# [START chat_quickstart]
16+
from __future__ import print_function
17+
18+
import os.path
19+
20+
from google.auth.transport.requests import Request
21+
from google.oauth2.credentials import Credentials
22+
from google_auth_oauthlib.flow import InstalledAppFlow
23+
from google.apps import chat_v1 as google_chat
24+
25+
26+
# If modifying these scopes, delete the file token.json.
27+
SCOPES = ['https://www.googleapis.com/auth/chat.spaces.readonly']
28+
29+
30+
def main():
31+
"""Shows basic usage of the Google Chat API.
32+
"""
33+
creds = None
34+
# The file token.json stores the user's access and refresh tokens, and is
35+
# created automatically when the authorization flow completes for the first
36+
# time.
37+
if os.path.exists('token.json'):
38+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
39+
# If there are no (valid) credentials available, let the user log in.
40+
if not creds or not creds.valid:
41+
if creds and creds.expired and creds.refresh_token:
42+
creds.refresh(Request())
43+
else:
44+
flow = InstalledAppFlow.from_client_secrets_file(
45+
'credentials.json', SCOPES)
46+
creds = flow.run_local_server(port=0)
47+
# Save the credentials for the next run
48+
with open('token.json', 'w') as token:
49+
token.write(creds.to_json())
50+
51+
try:
52+
# Create a client
53+
client = google_chat.ChatServiceClient(
54+
credentials = creds,
55+
client_options = {
56+
"scopes" : SCOPES
57+
}
58+
)
59+
60+
# Initialize request argument(s)
61+
request = google_chat.ListSpacesRequest(
62+
# Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)
63+
filter = 'space_type = "SPACE"'
64+
)
65+
66+
# Make the request
67+
page_result = client.list_spaces(request)
68+
69+
# Handle the response. Iterating over page_result will yield results and
70+
# resolve additional pages automatically.
71+
for response in page_result:
72+
print(response)
73+
except Exception as error:
74+
# TODO(developer) - Handle errors from Chat API.
75+
print(f'An error occurred: {error}')
76+
77+
78+
if __name__ == '__main__':
79+
main()
80+
# [END chat_quickstart]

chat/quickstart/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
google-apps-chat==0.1.0
2+
google-auth-httplib2==0.1.0
3+
google-auth-oauthlib==0.4.0

0 commit comments

Comments
 (0)