Peter
Peter

Reputation: 3495

Creating a Google Chat Space with a service account

I'm using the Google Chat API (google.apps.chat_v1). My intention is to write some code that will:

  1. Receive data from Pub/Sub with a message and email address
  2. Create a new DIRECT_MESSAGE space to that user if one doesn't already exist
  3. Send a chat message to the user

Sending messages to existing spaces works fine, but I just can't figure out to create a new space.

Here's the setup code I'm testing with:

from google.apps import chat_v1
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/chat.bot',
          'https://www.googleapis.com/auth/chat.spaces.create',
          'https://www.googleapis.com/auth/chat.memberships']

creds = service_account.Credentials.from_service_account_file('service-account.json')
client = chat_v1.ChatServiceClient(
    credentials=creds,
    client_options={'scopes': SCOPES },
)

client.set_up_space

The parameters for this allow you to define a space with members of any type, but all requests fail, as the method is not supported on service accounts.

According to the docs - To create a DM between the calling user and the calling app, set Space.singleUserBotDm to true and don't specify any memberships. But with the following code I still unsurprisingly get the not supported error:

>>> request = chat_v1.SetUpSpaceRequest(space=chat_v1.Space(space_type=chat_v1.Space.SpaceType.DIRECT_MESSAGE, single_user_bot_dm=True))
>>> client.set_up_space(request)
google.api_core.exceptions.PermissionDenied: 
    403 This method doesn't support service account authentication.
    Authenticate with a user account.

It indicates I must use the "calling app", but I'm not sure what this is. I checked the APIs and Services page and I couldn't see any other credentials to use, other than the OAuth ones which require running the InstalledAppFlow server (which is not an option for a web service as it loads up a "sign in with google" page as soon as you run it).


client.create_space

The other method says it only supports the SPACE type.

>>> request = chat_v1.CreateSpaceRequest(space=chat_v1.Space(space_type=chat_v1.Space.SpaceType.SPACE, display_name='Test Space'))
>>> client.create_space(request)
google.api_core.exceptions.InvalidArgument: 
    400 Missing or malformed resource name in the request.
    Make sure the customer id resource name follows this format: customers/{customer}'

CreateSpaceRequest only takes space as an argument, so I'm not even sure how you're supposed to use it. A search for any matching code on GitHub didn't help.


At this point I'm a bit stuck on what to do next. Would appreciate some pointers.

After typing this, I saw that on the Chat Auth page it says "app authentication" is not supported when creating spaces, but the documentation contradicts that by saying that the calling app is able to create a DM to a user, so I'm not at all sure what's possible and what isn't here.

Upvotes: 0

Views: 266

Answers (0)

Related Questions