Invite or add a user, Google Group, or Google Chat app to a space (original) (raw)

This guide explains how to use thecreate()method on the Membership resource of the Google Chat API to invite or add a user, Google Group, or Chat app to a space also known as creating a membership. When creating a membership, if the specified member has their auto-accept policy turned off, then they're invited, and must accept the space invitation before joining. Otherwise, creating a membership adds the member directly to the specified space.

If you're a Google Workspace administrator, you can add users, Google Groups, or Chat apps to any space in your Google Workspace organization.

TheMembership resourcerepresents whether a human user or Google Chat app is invited to, part of, or absent from a space.

Prerequisites

Node.js

Python

Java

Apps Script

To invite or add a user to a space withuser authentication, pass the following in your request:

The following example adds a user to a space withuser authentication:

Node.js

Python

Java

Apps Script

To run the sample, replace the following:

The Chat API returns an instance ofMembershipthat details the user membership that was created.

Invite or add a Google Group to a space

To invite or add a Google Group to a space withuser authentication(App authenticationdoesn't support inviting or adding a Google Group to a space), pass the following in your request:

Google Groups can't be added to a group chat or direct message, but only to a named space.

The following example adds a group to a named space with user authentication:

Node.js

Python

Java

Apps Script

To run the sample, replace the following:

The Chat API returns an instance ofMembershipthat details the user membership that was created.

Add a Chat app to a space

A Chat app can't add another app as a member to a space. To add a Chat app to a space or a direct message between two human users, pass the following in your request with user authentication(App authenticationdoesn't support inviting or adding a Chat app to a space):

The following example adds a Chat app to a space:

Node.js

Python

Java

Apps Script

To run the sample, replace SPACE_NAME with the ID from the space'sname. You can obtain the ID by calling theListSpaces()method or from the space's URL.

The Chat API returns an instance ofMembershipthat details the user membership that was created.

Invite or add a user to a space as a Chat app

App authentication requires one-timeadministrator approval.

To invite or add a user to a space withapp authentication, pass the following in your request:

Write a script that calls Chat API

The following example adds a user to a space withapp authentication:

Python

  1. In your working directory, create a file namedchat_membership_app_create.py.
  2. Include the following code in chat_membership_app_create.py:
from google.oauth2 import service_account  
from apiclient.discovery import build  
# Define your app's authorization scopes.  
# When modifying these scopes, delete the file token.json, if it exists.  
SCOPES = ["https://www.googleapis.com/auth/chat.app.memberships"]  
def main():  
    '''  
    Authenticates with Chat API using app authentication,  
    then adds a user to a Chat space by creating a membership.  
    '''  
    # Specify service account details.  
    creds = (  
        service_account.Credentials.from_service_account_file('credentials.json')  
        .with_scopes(SCOPES)  
    )  
    # Build a service endpoint for Chat API.  
    chat = build('chat', 'v1', credentials=creds)  
    # Use the service endpoint to call Chat API.  
    result = chat.spaces().members().create(  
        # The space in which to create a membership.  
        parent = 'spaces/SPACE',  
        # Specify which user the membership is for.  
        body = {  
          'member': {  
            'name':'users/USER',  
            'type': 'HUMAN'  
          }  
        }  
    ).execute()  
    # Prints details about the created membership.  
    print(result)  
if __name__ == '__main__':  
    main()  
  1. In the code, replace the following:
    • SPACE: a space name, which you can obtain from thespaces.list methodin the Chat API, or from a space's URL.
    • USER: a user ID.
  2. In your working directory, build and run the sample:
python3 chat_membership_app_create.py  

Add users or Google Groups to a space as a Google Workspace administrator

If you're a Google Workspace administrator, you can call the create()method to add users, Google Groups, or Chat apps to any space in your Google Workspace organization.

To call this method as a Google Workspace administrator, do the following:

For more information and examples, seeManage Google Chat spaces as a Google Workspace administrator.

Limitations and considerations