在 Google Chat 聊天室中更新用户的成员资格

本指南介绍了如何使用 update() Google Chat API 的 Membership 资源中的方法来更改成员资格的相关属性,例如将聊天室成员更改为聊天室管理员或 聊天室所有者。

如果您是 Google Workspace 管理员,则可以调用 update() 方法来更新 Google Workspace 组织中任何聊天室的成员资格。

The Membership 资源 表示人类用户或 Google Chat 应用是否受邀加入聊天室、 是否是聊天室的成员或是否缺席聊天室。

前提条件

Node.js

更新成员资格

如需更新聊天室成员资格,请在请求中传递以下内容:

  • 指定授权范围:
    • 使用 用户身份验证 时, 请指定 chat.memberships 授权范围。
    • 使用 应用身份验证时, 请指定 chat.app.memberships 授权范围。使用应用身份验证更新成员资格时,您只能更新 Chat 应用创建的聊天室中的成员资格。应用身份验证需要管理员一次性 批准
  • 调用 UpdateMembership() 方法。
  • membership 作为 Membership 的实例传递,并包含以下内容:
    • 设置为要更新的成员资格的 name 字段,其中包括聊天室 ID 和成员 ID。
    • 要更新的成员资格字段设置为新值。
  • 传递 updateMask 以指定要更新的成员资格方面,其中包括以下内容:
    • role:用户在 Chat 聊天室中的角色,决定了他们在聊天室中的允许操作。如需详细了解权限,请参阅 MembershipRole Chat API 参考文档中的 。可能的值包括:
      • ROLE_MEMBER:聊天室的成员。在 Chat 界面中,此角色称为成员
      • ROLE_ASSISTANT_MANAGER:聊天室管理员。在 Chat 界面中,此角色称为管理员
      • ROLE_MANAGER:聊天室所有者。在 Chat 界面中,此角色称为所有者

将成员更改为所有者(用户身份验证)

以下示例使用 用户身份验证 调用 Chat API,通过将 role 指定为 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);
}

await main();

如需运行示例,请替换以下内容:

  • SPACE_NAME:聊天室 name中的 ID。 您可以通过调用 ListSpaces() 方法或从聊天室的网址获取 ID。
  • MEMBER_NAME:成员资格 name中的 ID。 您可以通过调用 ListMemberships() 方法获取 ID,也可以从使用 Chat API 异步创建成员资格 后返回的响应正文中获取 ID。
  • ROLE_NAME:更新后的角色 ROLE_MANAGER。 您可以将此值设置为 MembershipRole的任何值。 例如,如需将普通成员更改为聊天室管理员,请将 ROLE_NAME 更改为 ROLE_ASSISTANT_MANAGER

Google Chat API 会将指定的成员资格更新为聊天室所有者,并返回 an instance of Membership

将所有者更改为成员(用户身份验证)

以下示例使用 用户身份验证 调用 Chat API,通过将 role 指定为 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);
}

await main();

如需运行示例,请替换以下内容:

  • SPACE_NAME:聊天室 name中的 ID。 您可以通过调用 ListSpaces() 方法或从聊天室的网址获取 ID。
  • MEMBER_NAME:成员资格 name中的 ID。 您可以通过调用 ListMemberships() 方法获取 ID,也可以从使用 Chat API 异步创建成员资格 后返回的响应正文中获取 ID。
  • ROLE_NAME:更新后的角色 ROLE_MEMBER

Google Chat API 会将指定的成员资格更新为聊天室所有者,并返回 an instance of Membership

将成员更改为所有者(Chat 应用身份验证)

应用身份验证需要管理员一次性 批准

编写调用 Chat API 的脚本

以下示例使用 应用身份验证 调用 Chat API,通过在指定更新后的成员资格属性的 body 中将 role 指定为 ROLE_MANAGER,将普通聊天室成员更改为聊天室所有者:

Python

  1. 在工作目录中,创建一个名为 chat_membership_update_to_owner_app.py 的文件。
  2. chat_membership_update_to_owner_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 owner.
        '''
    
        # 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().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',
    
            # Replace ROLE with a MembershipRole value.
            # Obtain the MembershipRole values from the membership of Chat API.
            body={'role': 'ROLE'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在代码中,替换以下内容:

  4. 在工作目录中,构建并运行示例:

    python3 chat_membership_update_to_owner_app.py

将所有者更改为成员(Chat 应用身份验证)

应用身份验证需要管理员一次性 批准

编写调用 Chat API 的脚本

以下示例使用 应用身份验证 调用 Chat API,通过在指定更新后的成员资格属性的 body 中将 role 指定为 ROLE_MEMBER,将聊天室所有者更改为普通聊天室成员:

Python

  1. 在工作目录中,创建一个名为 chat_membership_update_to_member_app.py 的文件。
  2. 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 owner to change
        it to a regular member.
        '''
    
        # 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().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. 在代码中,替换以下内容:

  4. 在工作目录中,构建并运行示例:

    python3 chat_membership_update_to_member_app.py

以 Google Workspace 管理员身份更新成员资格

如果您是 Google Workspace 管理员,则可以调用 update() 方法来更新 Google Workspace 组织中任何聊天室的成员资格。

如需以 Google Workspace 管理员身份调用此方法,请执行以下操作:

  • 使用用户身份验证调用该方法,并指定支持使用 管理员权限 调用该方法的 授权范围
  • 在请求中,将查询参数 useAdminAccess 指定为 true

如需了解详情和示例,请参阅 以 Google Workspace 管理员身份管理 Google Chat 聊天室