I am trying to access chat API to create spaces, groups, do messages and also add attachments for odoo. Now when I try it in python, even though I have followed each step suggested by google and community, I either get 403 or 404 error saying Google Chat app not found. To create a Chat app, you must turn on the Chat API and configure the app in the Google Cloud console. The steps I have followed.
- Created project in GCP
- enabled API by going to project > APIs & Services > Enabled APIs & services > google chat API > enable
- once enabled I see the button to manage it, Manage > Credentials > Create credentials > service Account.
- Once on service screen, I add name, grant roles, (owner, chat bots owner) > done.
- once service account is created, > Keys > add new key > download json and use it.
Having followed all above steps I am sure I am enabling API and using its key correctly but don't know it again says chat app not found.
I want to authorize it using a service account as I am trying to integrate it at the organization level.
Below is the code that I am using
from googleapiclient.discovery import build
from google.oauth2 import service_account
# Replace with your service account key file path
SERVICE_ACCOUNT_KEY_FILE = "./fluted-bee-418112-d595ff244760.json"
# Scopes for Chat API access
SCOPES = ['https://www.googleapis.com/auth/chat.bot', "https://www.googleapis.com/auth/chat.spaces.create", "https://www.googleapis.com/auth/chat.messages"] # "https://www.googleapis.com/auth/chat.spaces", "https://www.googleapis.com/auth/chat.spaces.create",
def create_space(credentials, name):
service = build("chat", "v1", credentials=credentials)
print(service)
body = {
"displayName": name,
"spaceType": "SPACE",
}
try:
response = service.spaces().create(body=body).execute()
print(f"Space created: {response['name']}")
# return response["name"]
except Exception as e:
print(f"Error creating space: {e}")
return None
def get_service_account_credentials():
credentials = service_account.Credentials.from_service_account_file(
filename=SERVICE_ACCOUNT_KEY_FILE,
scopes=SCOPES)
return credentials
if __name__ == "__main__":
# Get credentials using service account
credentials = get_service_account_credentials()
# Create space using authorized credentials
space_name = create_space(credentials, "My Space")
if not space_name:
exit()
Complete Error
<googleapiclient.discovery.Resource object at 0x00000151E77DD5B0>
Error creating space: <HttpError 404 when requesting https://chat.googleapis.com/v1/spaces?alt=json returned "Google Chat app not found. To create a Chat app, you must turn on the Chat API and configure the app in the Google Cloud console.". Details: "[{'@type': 'type.googleapis.com/google.rpc.Help', 'links': [{'description': 'To turn on the Google Chat API, go to the Google Chat API page in the Google Cloud console and click Enable.', 'url': 'https://console.cloud.google.com/marketplace/product/google/chat.googleapis.com'}]}, {'@type': 'type.googleapis.com/google.rpc.Help', 'links': [{'description': 'To configure your Chat app, go to the Google Chat API Configuration tab in the Google Cloud console.', 'url': 'https://console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat'}]}, {'@type': 'type.googleapis.com/google.rpc.Help', 'links': [{'description': 'To learn about creating Google Chat apps, visit the documentation.', 'url': 'https://developers.google.com/chat'}]}]">
I am unsure if you also configured the ChatAPI and forgot to mention here. But in case you haven't, you'll need to. To do so, go to Google Chat API, then click Manage > Configuration. And use this for reference.