取得會員資格詳情

本指南說明如何使用 Google Chat API Membership 資源的 get 方法,取得聊天室成員的詳細資料。

聊天室擁有者和管理員可以限制哪些人能查看聊天室的成員名單。如果瀏覽權限受限,透過「僅限應用程式驗證」呼叫這個方法時,系統只會傳回聊天室成員可見的成員資格,並針對聊天室成員不可見的成員資格傳回 PERMISSION_DENIED 錯誤。使用使用者驗證呼叫這個方法會傳回 PERMISSION_DENIED 錯誤。

如果您是 Google Workspace 管理員,可以呼叫 get 方法,擷取 Google Workspace 機構中任何成員的詳細資料。

Membership 資源代表使用者或 Google Chat 應用程式是否受邀加入、參與或缺席聊天室。

透過應用程式驗證進行驗證後,Chat 應用程式就能取得 Google Chat 中有權存取的聊天室成員資格 (例如應用程式是成員的聊天室),但會排除 Chat 應用程式成員資格,包括應用程式本身的成員資格。透過使用者驗證進行驗證,會傳回驗證使用者可存取的聊天室成員資格。

必要條件

Node.js

Python

Java

Apps Script

查看會員方案詳細資料

如要取得 Google Chat 會員資格的詳細資料,請在要求中傳遞下列項目:

  • 使用應用程式驗證,指定 chat.bot 授權範圍。透過使用者驗證,指定 chat.memberships.readonly 或 chat.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);
}

await main();

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 Script

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);
}

await main();

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 Script

chat/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 聊天室」。