4

I'm following this article to create a message in Google Chat via services account. However when I compile the code, I got the following message:

Traceback (most recent call last): File "googlechat-api.py", line 27, in resp = chat.spaces().messages().create( File "C:\Users\tdi5539\Anaconda3\lib\site-packages\googleapiclient_helpers.py", line 134, in positional_wrapper return wrapped(*args, **kwargs) File "C:\Users\tdi5539\Anaconda3\lib\site-packages\googleapiclient\http.py", line 935, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 404 when requesting https://chat.googleapis.com/v1/spaces/AAAAcE3QeHo/messages?alt=json returned "Invalid project number.". Details: "Invalid project number.">

Could anyone explain for me what is the problem?

Here is my code by the way:

from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
scopes = 'https://www.googleapis.com/auth/chat.bot'
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'credentials.json', scopes)
chat = build('chat', 'v1', http=credentials.authorize(Http()))
resp = chat.spaces().messages().create(
    parent='spaces/AAAAcE3QeHo', # use your space here
    body={'text': 'Test message'}).execute()
print(resp)
Sakshi Gatyan
  • 1,903
  • 7
  • 13
Nghĩa Lê
  • 53
  • 3
  • How did you come up with the file `credentials.json`? Can you check whether you can access the corresponding GCP project? – Iamblichus Jul 15 '21 at 08:55
  • 1
    Hi @Sakshi Gatyan, were you able to find out the root cause. I'm also facing the same issue, which shows "invalid project number" after making the API call – Jervis Lin Aug 02 '21 at 22:07

2 Answers2

0

You need to setup your Bot, and you can only setup Bots using Google Workspace aka GSuite accounts

In my case I started the project with a @gmail.com account and had to add a user from Google Workspace @mycompany.com to the project and setup the Bot from that account.

Link to setup Google Chat Bots should be something like: https://console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat?authuser=1&project=[your-project]&supportedpurview=project

Paulo Taylor
  • 716
  • 8
  • 18
0

in my case I had to use option.WithQuotaProject in golang, probably quota_project_id in python according to this page

Pega88
  • 564
  • 1
  • 6
  • 16