获取有关会员资格的详细信息

本指南介绍了如何对 Google Chat API 的 Membership 资源使用 get() 方法,以获取有关聊天室成员资格的详细信息。

如果您是 Google Workspace 管理员,则可以调用 get() 方法来检索有关您 Google Workspace 组织中任何成员资格的详细信息。

Membership 资源表示有人为用户或 Google Chat 应用发出了加入聊天室的邀请、用户或 Google Chat 应用已加入聊天室,还是未加入聊天室。

通过应用身份验证进行身份验证后,Chat 应用可以获取其在 Google Chat 中拥有访问权限的聊天室(例如,其是成员的聊天室)的成员资格,但不包括 Chat 应用成员资格,包括其自己的成员资格。使用用户身份验证进行身份验证会返回已验证用户有权访问的聊天室的会员资格。

前提条件

Node.js

Python

Java

Apps 脚本

获取有关会员的详细信息

如需获取有关 Google Chat 会员资格的详细信息,请在请求中传递以下内容:

  • 使用应用身份验证时,请指定 chat.bot 授权范围。使用用户身份验证时,请指定 chat.memberships.readonlychat.memberships 授权范围。最佳实践是,选择最严格的范围,但仍允许应用正常运行。
  • 调用 GetMembership() 方法。
  • 传递要获取的会员的 name。从 Google Chat 的成员资格资源中获取成员资格名称。

通过用户身份验证获取会员资格

以下是使用用户身份验证获取成员资格的方法:

Node.js

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

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

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

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MEMBER_NAME here
    name: 'spaces/SPACE_NAME/members/MEMBER_NAME'
  };

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

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

main().catch(console.error);

Python

chat/client-libraries/cloud/get_membership_user_cred.py
from authentication_utils import create_client_with_user_credentials
from google.apps import chat_v1 as google_chat

SCOPES = ["https://www.googleapis.com/auth/chat.memberships.readonly"]

# This sample shows how to get membership with user credential
def get_membership_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.GetMembershipRequest(
        # Replace SPACE_NAME and MEMBER_NAME here
        name = 'spaces/SPACE_NAME/members/MEMBER_NAME',
    )

    # Make the request
    response = client.get_membership(request)

    # Handle the response
    print(response)

get_membership_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMembershipUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetMembershipRequest;
import com.google.chat.v1.Membership;

// This sample shows how to get membership with user credential.
public class GetMembershipUserCred {

  private static final String SCOPE =
    "https://www.googleapis.com/auth/chat.memberships.readonly";

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      GetMembershipRequest.Builder request = GetMembershipRequest.newBuilder()
        // replace SPACE_NAME and MEMBERSHIP_NAME here
        .setName("spaces/SPACE_NAME/members/MEMBERSHIP_NAME");
      Membership response = chatServiceClient.getMembership(request.build());

      System.out.println(JsonFormat.printer().print(response));
    }
  }
}

Apps 脚本

chat/advanced-service/Main.gs
/**
 * This sample shows how to get membership with user credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.memberships.readonly'
 * referenced in the manifest file (appsscript.json).
 */
function getMembershipUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MEMBER_NAME here
  const name = 'spaces/SPACE_NAME/members/MEMBER_NAME';

  // Make the request
  const response = Chat.Spaces.Members.get(name);

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

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

  • SPACE_NAME:聊天室的 name 中的 ID。您可以通过调用 ListSpaces() 方法或从聊天室的网址获取该 ID。
  • MEMBER_NAME:成员 name 中的 ID。您可以通过调用 ListMemberships() 方法来获取 ID。

Chat API 会返回一个 Membership 实例,其中详细说明了指定成员资格。

通过应用身份验证获取会员资格

如需通过应用身份验证获取会员资格,请按以下步骤操作:

Node.js

chat/client-libraries/cloud/get-membership-app-cred.js
import {createClientWithAppCredentials} from './authentication-utils.js';

// This sample shows how to get membership with app credential
async function main() {
  // Create a client
  const chatClient = createClientWithAppCredentials();

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MEMBER_NAME here
    name: 'spaces/SPACE_NAME/members/MEMBER_NAME'
  };

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

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

main().catch(console.error);

Python

chat/client-libraries/cloud/get_membership_app_cred.py
from authentication_utils import create_client_with_app_credentials
from google.apps import chat_v1 as google_chat

# This sample shows how to get membership with app credential
def get_membership_with_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.GetMembershipRequest(
        # Replace SPACE_NAME and MEMBER_NAME here
        name = 'spaces/SPACE_NAME/members/MEMBER_NAME',
    )

    # Make the request
    response = client.get_membership(request)

    # Handle the response
    print(response)

get_membership_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMembershipAppCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetMembershipRequest;
import com.google.chat.v1.Membership;

// This sample shows how to get membership with app credential.
public class GetMembershipAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      GetMembershipRequest.Builder request = GetMembershipRequest.newBuilder()
        // replace SPACE_NAME and MEMBERSHIP_NAME here
        .setName("spaces/SPACE_NAME/members/MEMBERSHIP_NAME");
      Membership response = chatServiceClient.getMembership(request.build());

      System.out.println(JsonFormat.printer().print(response));
    }
  }
}

Apps 脚本

聊天/advanced-service/Main.gs
/**
 * This sample shows how to get membership with app credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function getMembershipAppCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MEMBER_NAME here
  const name = 'spaces/SPACE_NAME/members/MEMBER_NAME';
  const parameters = {};

  // Make the request
  const response = Chat.Spaces.Members.get(name, parameters, getHeaderWithAppCredentials());

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

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

  • SPACE_NAME:聊天室的 name 中的 ID。您可以通过调用 ListSpaces() 方法或从聊天室的网址获取该 ID。
  • MEMBER_NAME:成员 name 中的 ID。您可以通过调用 ListMemberships() 方法来获取 ID。

Chat API 会返回一个 Membership 实例,其中详细说明了指定的成员资格。

详细了解 Google Workspace 管理员的会员资格

如果您是 Google Workspace 管理员,则可以调用 GetMembership() 方法来检索 Google Workspace 组织中任何用户的会员资格详细信息。

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

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

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