Update a user's membership in a Google Chat space

This guide explains how to use the update() method on the Membership resource of the Google Chat API to change attributes about a membership, like changing a space member to a space manager, or changing a space manager to a space member.

If you're a Google Workspace administrator, you can call the update() method to update any space's membership in your Google Workspace organization.

The Membership resource represents whether a human user or Google Chat app is invited to, part of, or absent from a space.

Prerequisites

Node.js

Update a membership

To update a space membership, pass the following in your request:

  • Specify an authorization scope:
  • Call the UpdateMembership() method.
  • Pass membership as an instance of Membership with the following:
    • The name field set to the membership to update, which includes a space ID and a member ID.
    • The membership fields to update set to the new values.
  • Pass updateMask to specify the aspects of the membership to update, it includes the following:
    • role: User's role within a Chat space, which determines their permitted actions in the space. Possible values are:
      • ROLE_MEMBER: A member of the space. The user has basic permissions, like sending messages to the space. In 1:1 and unnamed group conversations, everyone has this role.
      • ROLE_MANAGER: A space manager. The user has all basic permissions plus administrative permissions that let them manage the space, like adding or removing members. Only supported in spaces where spaceType is SPACE (named spaces).

Make a regular space member a space manager as a user

The following example calls the Chat API using user authentication to make a regular space member a space manager by specifying role as ROLE_MANAGER:

Node.js

chat/client-libraries/cloud/update-membership-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.memberships'];

// This sample shows how to update a membership with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    membership: {
      // Replace SPACE_NAME and MEMBER_NAME here
      name: 'spaces/SPACE_NAME/members/MEMBER_NAME',
      // Replace ROLE_NAME here with ROLE_MEMBER or ROLE_MANAGER
      role: 'ROLE_NAME'
    },
    updateMask: {
      // The field paths to update.
      paths: ['role']
    }
  };

  // Make the request
  const response = await chatClient.updateMembership(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

To run the sample, replace the following:

  • SPACE_NAME: the ID from the space's name. You can obtain the ID by calling the ListSpaces() method or from the space's URL.
  • MEMBER_NAME: the ID from the membership's name. You can obtain the ID by calling the ListMemberships() method, or from the response body returned after creating a membership asynchronously with the Chat API.
  • ROLE_NAME: the updated role, ROLE_MANAGER.

The Google Chat API updates the specified membership to a space manager and returns an instance of Membership.

Make a space manager a regular member as a user

The following example calls the Chat API using user authentication to make a space manager a regular space member by specifying role as ROLE_MEMBER:

Node.js

chat/client-libraries/cloud/update-membership-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.memberships'];

// This sample shows how to update a membership with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    membership: {
      // Replace SPACE_NAME and MEMBER_NAME here
      name: 'spaces/SPACE_NAME/members/MEMBER_NAME',
      // Replace ROLE_NAME here with ROLE_MEMBER or ROLE_MANAGER
      role: 'ROLE_NAME'
    },
    updateMask: {
      // The field paths to update.
      paths: ['role']
    }
  };

  // Make the request
  const response = await chatClient.updateMembership(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

To run the sample, replace the following:

  • SPACE_NAME: the ID from the space's name. You can obtain the ID by calling the ListSpaces() method or from the space's URL.
  • MEMBER_NAME: the ID from the membership's name. You can obtain the ID by calling the ListMemberships() method, or from the response body returned after creating a membership asynchronously with the Chat API.
  • ROLE_NAME: the updated role, ROLE_MEMBER.

The Google Chat API updates the specified membership to a space manager and returns an instance of Membership.

Make a regular space member a space manager as a Chat app

App authentication requires one-time administrator approval.

Create an API key

To call a Developer Preview API method, you must use a non-public developer preview version of the API discovery document. To authenticate the request, you must pass an API key.

To create the API Key, open your app's Google Cloud project and do the following:

  1. In the Google Cloud console, go to Menu > APIs & Services > Credentials.

    Go to Credentials

  2. Click Create credentials > API key.
  3. Your new API key is displayed.
    • Click Copy to copy your API key for use in your app's code. The API key can also be found in the "API keys" section of your project's credentials.
    • Click Restrict key to update advanced settings and limit use of your API key. For more details, see Applying API key restrictions.

Write a script that calls the Chat API

The following example calls the Chat API using app authentication to make a regular space member a space manager by specifying role as ROLE_MANAGER in the body that specifies updated membership attributes:

Python

  1. In your working directory, create a file named chat_membership_update_to_manager_app.py.
  2. Include the following code in chat_membership_update_to_manager_app.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 updates a specified space member to change
        it from a regular member to a space manager.
        '''
    
        # 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, discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY')
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().members().patch(
    
            # The membership to update, and the updated role.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBERSHIP with a membership name.
            # Obtain the membership name from the membership of Chat API.
            name='spaces/SPACE/members/MEMBERSHIP',
            updateMask='role',
            body={'role': 'ROLE_MANAGER'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. In the code, replace the following:

    • API_KEY: the API key you created to build the service endpoint for Chat API.

    • SPACE: a space name, which you can obtain from the spaces.list method in the Chat API, or from a space's URL.

    • MEMBERSHIP: a membership name, which you can obtain from the spaces.members.list method in the Chat API.

  4. In your working directory, build and run the sample:

    python3 chat_membership_update_to_manager_app.py

Make a space manager a regular member as a Chat app

App authentication requires one-time administrator approval.

Create an API key

To call a Developer Preview API method, you must use a non-public developer preview version of the API discovery document. To authenticate the request, you must pass an API key.

To create the API Key, open your app's Google Cloud project and do the following:

  1. In the Google Cloud console, go to Menu > APIs & Services > Credentials.

    Go to Credentials

  2. Click Create credentials > API key.
  3. Your new API key is displayed.
    • Click Copy to copy your API key for use in your app's code. The API key can also be found in the "API keys" section of your project's credentials.
    • Click Restrict key to update advanced settings and limit use of your API key. For more details, see Applying API key restrictions.

Write a script that calls the Chat API

The following example calls the Chat API using app authentication to make a space manager a regular space member by specifying role as ROLE_MEMBER in the body that specifies updated membership attributes:

Python

  1. In your working directory, create a file named chat_membership_update_to_member_app.py.
  2. Include the following code in chat_membership_update_to_member_app.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 via user credentials,
        then updates a specified space member to change
        it from a regular member to a space manager.
        '''
    
        # 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, discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY')
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().members().patch(
    
            # The membership to update, and the updated role.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBERSHIP with a membership name.
            # Obtain the membership name from the membership of Chat API.
            name='spaces/SPACE/members/MEMBERSHIP',
            updateMask='role',
            body={'role': 'ROLE_MEMBER'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. In the code, replace the following:

    • API_KEY: the API key you created to build the service endpoint for Chat API.

    • SPACE: a space name, which you can obtain from the spaces.list method in the Chat API, or from a space's URL.

    • MEMBERSHIP: a membership name, which you can obtain from the spaces.members.list method in the Chat API.

  4. In your working directory, build and run the sample:

    python3 chat_membership_update_to_member_app.py

Update memberships as a Google Workspace administrator

If you're a Google Workspace administrator, you can call the update() method to update memberships for any space in your Google Workspace organization.

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

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