|
| 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] |
0 commit comments