googleapis / google-api-python-client

🐍 The official Python client library for Google's discovery based APIs.
https://googleapis.github.io/google-api-python-client/docs/
Apache License 2.0
7.77k stars 2.41k forks source link

GChat APIs stopped working #1608

Closed Rahul2702 closed 2 years ago

Rahul2702 commented 2 years ago

Environment details

Code example

from google.oauth2 import service_account                                                       
from googleapiclient.discovery import build

SCOPES = ['https://www.googleapis.com/auth/admin.directory.orgunit',
          'https://www.googleapis.com/auth/admin.reports.audit.readonly',
          'https://www.googleapis.com/auth/admin.directory.user.readonly',
          'https://www.googleapis.com/auth/chat.bot']

cnfFilePath = './conf/service_account_credentials.json'

def _create_credentials(subject):
    if not subject: return None
    credentials = service_account.Credentials
    credentials = credentials.from_service_account_file(cnfFilePath, scopes=SCOPES)
    credentials = credentials.with_subject(subject)
    return credentials

class GoogleChatService:
    def __init__(self, userEmail):
        self.actor = userEmail
        self.credentials = _create_credentials(self.actor)
        if not self.credentials:
            raise Exception("Failed to create GoogleChatService for actor: {}"
                            .format(self.actor))
        self.service = build('chat', 'v1', credentials=self.credentials)

    def get_roomid_members(self, roomid):
        """
        :param roomid:
        :return: list of members
        """
        result = self.service.spaces().members().list(parent='spaces/' + roomid).execute()
        print("Result", result)
        return result.get('memberships', [])

    def get_message(self, roomid, msgid):
        """
        Get actual message using chat api
        :param msgid: msgid of the message
        :return: message of format https://developers.google.com/chat/reference/rest/v1/spaces.messages#Message
        """
        messages = "{}.{}".format(msgid, msgid)
        name = u'spaces/{0}/messages/{1}'.format(roomid, messages)
        chat_resp = self.service.spaces().messages().get(name=name).execute()
        return chat_resp

    def get_spaces(self):
        """
        :param roomid:
        :return: list of members
        """
        result = self.service.spaces().list().execute()
        print("Result", result)
        return result.get('memberships', [])

if __name__ == "__main__":
    userEmail = 'admin@xyz.com' #valid email address
    roomId = 'kekodwAAAAE'
    messageId = 'kekodwAAAAE'
    service = GoogleChatService(userEmail)
    service.get_roomid_members(roomId)
    service.get_message(roomId, messageId)
    service.get_spaces()

The above code was working till 27 Oct. 2021. After that its starts giving the below error. service_account_credentials.json is the Google App client service account with enabling GChat APIs, AdminASK API, and Audit APIs. I am trying to access the regular user(email address) GChat messages/room_members using delegated permissions.

Stack trace

Traceback (most recent call last):
  File "api.py", line 66, in <module>
    service.get_roomid_members(roomId)
  File "api.py", line 35, in get_roomid_members
    result = self.service.spaces().members().list(parent='spaces/' + roomid).execute()
  File "/home/rahul/securly_quarantine_venv/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 131, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/rahul/securly_quarantine_venv/lib/python3.8/site-packages/googleapiclient/http.py", line 937, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://chat.googleapis.com/v1/spaces/kekodwAAAAE/members?alt=json returned "The API is called without service account credentials. Instructions on how to set this up can be found at https://developers.google.com/hangouts/chat/how-tos/service-accounts". Details: "The API is called without service account credentials. Instructions on how to set this up can be found at https://developers.google.com/hangouts/chat/how-tos/service-accounts">

Thanks!

busunkim96 commented 2 years ago

Hi @Rahul2702,

The code looks correct from the google-api-python-client perspective. Since you said it worked until October 27, something may have changed in the service itself.

Some quick things to try:

If those steps don't narrow down what the issue is, please use one of the support channels listed on https://developers.google.com/chat/support and a Google Chat API expert will be able to help you.

Thanks!