แสดงรายการสมาชิกในพื้นที่ทำงาน

คู่มือนี้อธิบายวิธีใช้เมธอด list() ในทรัพยากร Membership ของ Google Chat API เพื่อแสดงรายชื่อสมาชิกในพื้นที่ทำงาน เป็นรายการการเป็นสมาชิกในพื้นที่ทำงานที่กรองได้และมีการแบ่งหน้า

  • การแสดงการเป็นสมาชิกด้วย การตรวจสอบสิทธิ์แอป จะแสดงการเป็นสมาชิกในพื้นที่ที่แอป Chat มี สิทธิ์เข้าถึง แต่จะไม่รวมการเป็นสมาชิกของแอป Chat รวมถึงการเป็นสมาชิกของแอปเอง
  • การแสดงการเป็นสมาชิกที่มี การตรวจสอบสิทธิ์ผู้ใช้ จะแสดงการเป็นสมาชิกในพื้นที่ที่ผู้ใช้ที่ตรวจสอบสิทธิ์แล้วมีสิทธิ์เข้าถึง
  • การแสดงรายการการเป็นสมาชิกในฐานะผู้ดูแลระบบ Google Workspace ที่มีการตรวจสอบสิทธิ์ผู้ใช้โดยใช้สิทธิ์ของผู้ดูแลระบบจะแสดงรายการการเป็นสมาชิกในพื้นที่ทำงานทั้งหมดในองค์กร Google Workspace ของคุณ

ออบเจ็กต์ Membership แสดงว่าผู้ใช้ที่เป็นมนุษย์หรือแอป Google Chat ได้รับเชิญให้เข้าร่วม เป็นส่วนหนึ่งของ หรือไม่ได้อยู่ในพื้นที่ทำงาน

ข้อกำหนดเบื้องต้น

Node.js

  • บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat

Python

  • บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat

Java

  • บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat

Apps Script

  • บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat

แสดงรายชื่อสมาชิกในพื้นที่ทำงานที่มีการตรวจสอบสิทธิ์ผู้ใช้

หากต้องการแสดงรายชื่อผู้ใช้ กลุ่ม Google และแอป Chat ในพื้นที่ ที่ผู้ใช้ที่ได้รับการตรวจสอบสิทธิ์มีสิทธิ์เข้าถึง ให้ส่งข้อมูลต่อไปนี้ในคำขอ

  • เมื่อใช้การตรวจสอบสิทธิ์ของผู้ใช้ ให้ระบุขอบเขตchat.memberships.readonlyหรือchat.membershipsการให้สิทธิ์
  • เรียกใช้เมธอด ListMemberships()
  • หากต้องการแสดงรายการ Google Groups ให้ตั้งค่าพารามิเตอร์การค้นหา showGroups เป็น true

ตัวอย่างต่อไปนี้แสดงรายชื่อสมาชิกใน Google Groups, สมาชิกที่เป็นบุคคล และสมาชิกที่เป็นแอปที่ผู้ใช้ที่ได้รับการตรวจสอบสิทธิ์มองเห็น

Node.js

chat/client-libraries/cloud/list-memberships-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 list memberships 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 here
    parent: 'spaces/SPACE_NAME',
    // Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
    // ROLE_MANAGER)
    filter: 'member.type = "HUMAN"'
  };

  // Make the request
  const pageResult = chatClient.listMembershipsAsync(request);

  // Handle the response. Iterating over pageResult will yield results and
  // resolve additional pages automatically.
  for await (const response of pageResult) {
    console.log(response);
  }
}

main().catch(console.error);

Python

chat/client-libraries/cloud/list_memberships_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 list memberships with user credential
def list_memberships_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.ListMembershipsRequest(
        # Replace SPACE_NAME here
        parent = 'spaces/SPACE_NAME',
        # Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
        # ROLE_MANAGER)
        filter = 'member.type = "HUMAN"',
        # Number of results that will be returned at once
        page_size = 100
    )

    # Make the request
    page_result = client.list_memberships(request)

    # Handle the response. Iterating over page_result will yield results and
    # resolve additional pages automatically.
    for response in page_result:
        print(response)

list_memberships_user_cred()

Java

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

// This sample shows how to list memberships with user credential.
public class ListMembershipsUserCred {

  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))) {
      ListMembershipsRequest.Builder request = ListMembershipsRequest.newBuilder()
        // Replace SPACE_NAME here.
        .setParent("spaces/SPACE_NAME")
        // Filter membership by type (HUMAN or BOT) or role
        // (ROLE_MEMBER or ROLE_MANAGER).
        .setFilter("member.type = \"HUMAN\"")
        // Number of results that will be returned at once.
        .setPageSize(10);

      // Iterating over results and resolve additional pages automatically.
      for (Membership response :
          chatServiceClient.listMemberships(request.build()).iterateAll()) {
        System.out.println(JsonFormat.printer().print(response));
      }
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to list memberships 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 listMembershipsUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here
  const parent = 'spaces/SPACE_NAME';
  // Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
  // ROLE_MANAGER)
  const filter = 'member.type = "HUMAN"';

  // Iterate through the response pages using page tokens
  let responsePage;
  let pageToken = null;
  do {
    // Request response pages
    responsePage = Chat.Spaces.Members.list(parent, {
      filter: filter,
      pageSize: 10,
      pageToken: pageToken
    });
    // Handle response pages
    if (responsePage.memberships) {
      responsePage.memberships.forEach((membership) => console.log(membership));
    }
    // Update the page token to the next one
    pageToken = responsePage.nextPageToken;
  } while (pageToken);
}

หากต้องการเรียกใช้ตัวอย่างนี้ ให้แทนที่ SPACE_NAME ด้วยรหัสจาก ฟิลด์ name ของพื้นที่ทำงาน คุณรับรหัสได้โดยการเรียกใช้เมธอด ListSpaces() หรือจาก URL ของพื้นที่ทำงาน

Google Chat API จะแสดงรายการสมาชิกที่เป็นกลุ่ม Google, มนุษย์ และแอปจากพื้นที่ที่ระบุ

แสดงรายชื่อสมาชิกในพื้นที่ทำงานที่มีการตรวจสอบสิทธิ์แอป

หากต้องการแสดงรายชื่อผู้ใช้และแอปใน Chat ในพื้นที่ ที่แอปที่ตรวจสอบสิทธิ์แล้วมีสิทธิ์เข้าถึง ให้ส่งข้อมูลต่อไปนี้ในคำขอ

  • เมื่อใช้การตรวจสอบสิทธิ์แอป ให้ระบุchat.botขอบเขตการให้สิทธิ์
  • เรียกใช้เมธอด ListMemberships()
  • หากต้องการแสดงรายการ Google Groups ให้ตั้งค่าพารามิเตอร์การค้นหา showGroups เป็น true

ตัวอย่างต่อไปนี้แสดงรายชื่อสมาชิกที่เป็นบุคคลในพื้นที่ทำงาน (ไม่ใช่ผู้จัดการพื้นที่ทำงาน) ที่แอป Chat มองเห็น

Node.js

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

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

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here
    parent: 'spaces/SPACE_NAME',
    // Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
    // ROLE_MANAGER)
    filter: 'member.type = "HUMAN"'
  };

  // Make the request
  const pageResult = chatClient.listMembershipsAsync(request);

  // Handle the response. Iterating over pageResult will yield results and
  // resolve additional pages automatically.
  for await (const response of pageResult) {
    console.log(response);
  }
}

main().catch(console.error);

Python

chat/client-libraries/cloud/list_memberships_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 list memberships with app credential
def list_memberships_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.ListMembershipsRequest(
        # Replace SPACE_NAME here
        parent = 'spaces/SPACE_NAME',
        # Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
        # ROLE_MANAGER)
        filter = 'member.type = "HUMAN"',
        # Number of results that will be returned at once
        page_size = 100
    )

    # Make the request
    page_result = client.list_memberships(request)

    # Handle the response. Iterating over page_result will yield results and
    # resolve additional pages automatically.
    for response in page_result:
        print(response)

list_memberships_app_cred()

Java

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

// This sample shows how to list memberships with app credential.
public class ListMembershipsAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      ListMembershipsRequest.Builder request = ListMembershipsRequest.newBuilder()
        // Replace SPACE_NAME here.
        .setParent("spaces/SPACE_NAME")
        // Filter membership by type (HUMAN or BOT) or role
        // (ROLE_MEMBER or ROLE_MANAGER).
        .setFilter("member.type = \"HUMAN\"")
        // Number of results that will be returned at once.
        .setPageSize(10);

      // Iterate over results and resolve additional pages automatically.
      for (Membership response :
          chatServiceClient.listMemberships(request.build()).iterateAll()) {
        System.out.println(JsonFormat.printer().print(response));
      }
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to list memberships with app credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function listMembershipsAppCred() {
// Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here
  const parent = 'spaces/SPACE_NAME';
  // Filter membership by type (HUMAN or BOT) or role (ROLE_MEMBER or
  // ROLE_MANAGER)
  const filter = 'member.type = "HUMAN"';

  // Iterate through the response pages using page tokens
  let responsePage;
  let pageToken = null;
  do {
    // Request response pages
    responsePage = Chat.Spaces.Members.list(parent, {
      filter: filter,
      pageSize: 10,
      pageToken: pageToken
    }, getHeaderWithAppCredentials());
    // Handle response pages
    if (responsePage.memberships) {
      responsePage.memberships.forEach((membership) => console.log(membership));
    }
    // Update the page token to the next one
    pageToken = responsePage.nextPageToken;
  } while (pageToken);
}

หากต้องการเรียกใช้ตัวอย่างนี้ ให้แทนที่ SPACE_NAME ด้วยรหัสจาก ฟิลด์ name ของพื้นที่ทำงาน คุณรับรหัสได้โดยการเรียกใช้เมธอด ListSpaces() หรือจาก URL ของพื้นที่ทำงาน

Google Chat API จะแสดงรายการ สมาชิกที่เป็นบุคคลในพื้นที่ทำงาน (ไม่รวมผู้จัดการพื้นที่ทำงาน) จากพื้นที่ทำงานที่ระบุ

แสดงรายชื่อสมาชิกในฐานะผู้ดูแลระบบ Google Workspace

หากคุณเป็นผู้ดูแลระบบ Google Workspace คุณสามารถเรียกใช้เมธอด ListMemberships() เพื่อแสดงรายการการเป็นสมาชิกของพื้นที่ทำงานในองค์กร Google Workspace ได้ Chat API จะแสดงเฉพาะ การเป็นสมาชิกเกี่ยวกับผู้ใช้ทั้งภายในและภายนอก หรือ Google Groups จาก องค์กรของคุณเท่านั้น จึงไม่แสดงการเป็นสมาชิกสำหรับแอปใน Chat

หากต้องการเรียกใช้เมธอดนี้ในฐานะผู้ดูแลระบบ Google Workspace ให้ทำดังนี้

  • เรียกใช้เมธอดโดยใช้การตรวจสอบสิทธิ์ผู้ใช้ และระบุขอบเขตการให้สิทธิ์ ที่รองรับการเรียกใช้เมธอดโดยใช้สิทธิ์ของผู้ดูแลระบบ
  • ในคำขอ ให้ระบุพารามิเตอร์การค้นหาต่อไปนี้
    • ตั้งค่า useAdminAccess เป็น true
    • หากต้องการแสดงเฉพาะผู้ใช้ ให้ตั้งค่า filter สำหรับ member.type เท่ากับ HUMAN
    • หากต้องการแสดงผู้ใช้และกลุ่ม ให้ตั้งค่า filter สำหรับ member.type ไม่ เท่ากับ BOT AND showGroups เท่ากับ true

ดูข้อมูลเพิ่มเติมและตัวอย่างได้ที่หัวข้อจัดการพื้นที่ใน Google Chat ในฐานะผู้ดูแลระบบ Google Workspace

ปรับแต่งการแบ่งหน้าหรือกรองรายการ

หากต้องการแสดงการเป็นสมาชิก ให้ส่งพารามิเตอร์การค้นหาต่อไปนี้เพื่อ ปรับแต่งการแบ่งหน้าหรือกรองการเป็นสมาชิกที่แสดง

  • pageSize: จำนวนการเป็นสมาชิกสูงสุดที่จะแสดง บริการอาจ แสดงผลน้อยกว่าค่านี้ หากไม่ได้ระบุ ระบบจะแสดงสเปซไม่เกิน 100 รายการ ค่าสูงสุดคือ 1,000 และระบบจะเปลี่ยนค่าที่มากกว่า 1,000 เป็น 1,000 โดยอัตโนมัติ
  • pageToken: โทเค็นหน้าเว็บที่ได้รับจากการเรียกใช้ list spaces ก่อนหน้า ระบุโทเค็นนี้เพื่อดึงข้อมูลหน้าถัดไป เมื่อแบ่งหน้า ค่าตัวกรองควรตรงกับการเรียกที่ให้โทเค็นหน้าเว็บ การส่งค่าอื่นอาจทำให้เกิดผลลัพธ์ที่ไม่คาดคิด
  • filter: ตัวกรองการค้นหา ต้องมีการตรวจสอบสิทธิ์ของผู้ใช้ ดูรายละเอียดการค้นหาที่รองรับได้ในข้อมูลอ้างอิงของ ListMembershipsRequest