1

I was using the post here: https://developers.google.com/chat/how-tos/service-accounts#creating_and_using_a_service_account to interact with Google Chat API. I've created the service account + enabled the API access as well as installing all the python dependencies/libraries.

However, I'm getting the following errors:

Traceback (most recent call last):
  File "a.py", line 9, in <module>
    body={'text': 'Test message'}).execute()
  File "/usr/local/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 131, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/googleapiclient/http.py", line 937, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://chat.googleapis.com/v1/spaces/AAAAWtx0J9a/messages?alt=json returned "Invalid project number.". Details: "Invalid project number.">

Code:

from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
scopes = 'https://www.googleapis.com/auth/chat.bot'
credentials = ServiceAccountCredentials.from_json_keyfile_name('gcapi-13221-bcbc7fe2b54b.json', scopes)
chat = build('chat', 'v1', http=credentials.authorize(Http()))
resp = chat.spaces().messages().create(
    parent='spaces/AAAAWtx0J9a',
    body={'text': 'Test message'}).execute()
print(resp)

Could anyone please shed some light on what I'm missing here?

Many thanks

Jervis Lin
  • 151
  • 1
  • 3
  • 11
  • What happened when you tried putting `googleapiclient.errors.HttpError Invalid project number` into a search engine? – Karl Knechtel Aug 03 '21 at 01:04
  • @KarlKnechtel, I couldn't find any explanation on google. I wonder if this API only works for google workspace – Jervis Lin Aug 03 '21 at 01:23
  • How did you come up with the file credentials.json? Can you check whether you can access the corresponding GCP project? – Rafa Guillermo Aug 03 '21 at 08:37
  • @RafaGuillermo that file is generated by following the steps in the link I attached in this post. – Jervis Lin Aug 03 '21 at 12:11
  • Are you sure you have downloaded the correct credentials? Did you have multiple Google accounts signed in when you did this? – Rafa Guillermo Aug 09 '21 at 09:17
  • Check my answer here: https://stackoverflow.com/questions/68313592/invalid-project-number-when-using-google-chat-api/69443219#69443219 – Paulo Taylor Oct 04 '21 at 23:05

0 Answers0