Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Hướng dẫn này giải thích cách sử dụng phương thức list() trên tài nguyên SpaceEvent của Google Chat API để liệt kê các thay đổi đối với tài nguyên trong một không gian.
Tài nguyên SpaceEvent biểu thị một thay đổi đối với không gian mục tiêu, bao gồm cả các tài nguyên con của không gian, chẳng hạn như tin nhắn, lượt bày tỏ cảm xúc và gói thành viên. Để biết thêm thông tin về danh sách các loại sự kiện và tải trọng sự kiện được hỗ trợ, hãy xem các trường eventType và payload của tài liệu tham khảo về tài nguyên SpaceEvent.
Bạn có thể liệt kê các sự kiện trong vòng 28 ngày trước thời điểm yêu cầu. Máy chủ trả về các sự kiện chứa phiên bản mới nhất của tài nguyên bị ảnh hưởng.
Ví dụ: nếu bạn liệt kê các sự kiện về thành viên mới trong không gian, thì máy chủ sẽ trả về các tài nguyên Membership chứa thông tin mới nhất về tư cách thành viên. Nếu các thành viên mới bị xoá trong khoảng thời gian được yêu cầu, thì tải trọng sự kiện sẽ chứa một tài nguyên Membership trống.
Để gọi phương thức này, bạn phải sử dụng xác thực người dùng. Để liệt kê các sự kiện trong một không gian, người dùng đã xác thực phải là thành viên của không gian đó.
Tạo thông tin đăng nhập mã ứng dụng khách OAuth cho một ứng dụng dành cho máy tính. Để chạy mẫu trong hướng dẫn này, hãy lưu thông tin đăng nhập dưới dạng tệp JSON có tên là credentials.json vào thư mục cục bộ của bạn.
Để được hướng dẫn, hãy hoàn tất các bước thiết lập môi trường trong phần hướng dẫn nhanh này.
Để liệt kê các sự kiện trong không gian của một không gian Chat, hãy truyền các thông tin sau trong yêu cầu của bạn:
Chỉ định một hoặc nhiều phạm vi uỷ quyền để hỗ trợ từng loại sự kiện trong yêu cầu của bạn. Tốt nhất là bạn nên chọn phạm vi hạn chế nhất nhưng vẫn cho phép ứng dụng của bạn hoạt động. Để chọn một phạm vi, hãy xem phần Tổng quan về quy trình xác thực và uỷ quyền.
import{createClientWithUserCredentials}from'./authentication-utils.js';// Authorization scopes based on the event typesconstUSER_AUTH_OAUTH_SCOPES=['https://www.googleapis.com/auth/chat.memberships.readonly','https://www.googleapis.com/auth/chat.messages.readonly'];// This sample shows how to list space events with user credentialasyncfunctionmain(){// Create a clientconstchatClient=awaitcreateClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);// Initialize request argument(s)constrequest={// Replace SPACE_NAME hereparent:'spaces/SPACE_NAME',// A required filter. Filters events about new memberships and messagesfilter:'eventTypes:"google.workspace.chat.membership.v1.created" OR eventTypes:"google.workspace.chat.message.v1.created"'};// Make the requestconstpageResult=chatClient.listSpaceEventsAsync(request);// Handle the response. Iterating over pageResult will yield results and// resolve additional pages automatically.forawait(constresponseofpageResult){console.log(response);}}main().catch(console.error);
Để chạy mẫu này, hãy thay thế SPACE_NAME bằng mã nhận dạng của name của không gian.
Bạn có thể lấy mã nhận dạng bằng cách gọi phương thức ListSpaces() hoặc từ URL của không gian.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-04 UTC."],[[["\u003cp\u003eThis guide explains how to use the \u003ccode\u003elist()\u003c/code\u003e method to list changes to resources in a Google Chat space using the \u003ccode\u003eSpaceEvent\u003c/code\u003e resource.\u003c/p\u003e\n"],["\u003cp\u003eYou can list events up to 28 days prior, retrieving the most recent version of affected resources, and must authenticate with user credentials and be a space member.\u003c/p\u003e\n"],["\u003cp\u003eTo call this method, specify authorization scopes for desired event types and use the \u003ccode\u003eListSpaceEvents()\u003c/code\u003e method with a filter to specify event types and date ranges.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Node.js code sample demonstrates listing events for new memberships and messages within a space, requiring replacement of \u003ccode\u003eSPACE_NAME\u003c/code\u003e with the space's ID.\u003c/p\u003e\n"]]],["This guide details how to use the `list()` method on the `SpaceEvent` resource within the Google Chat API to track changes in a space, such as new memberships and messages. To use, authenticate as a space member and specify the desired event types using the `filter` parameter when calling `ListSpaceEvents()`. The method returns the latest resource versions, up to 28 days prior, with the ability to filter by date. An example is provided showing how to filter events about new memberships and messages.\n"],null,["# List events from a Google Chat space\n\nThis guide explains how to use the\n[`list()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.ListSpaceEvents)\nmethod on the `SpaceEvent` resource of the Google Chat API to list changes to\nresources in a space.\n\nThe\n[`SpaceEvent` resource](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.SpaceEvent)\nrepresents a change to the target space, including child resources of the space\nsuch as messages, reactions, and memberships. For more information about the\nlist of event types and event payloads supported, see the\n[`eventType`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.SpaceEvent.FIELDS.string.google.chat.v1.SpaceEvent.event_type)\nand\n[`payload`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.SpaceEvent.FIELDS.oneof_payload)\nfields of the `SpaceEvent` resource reference documentation.\n\nYou can list events up to 28 days before the time of the request. The server\nreturns events that contain the most recent version of the affected resource.\nFor example, if you list events about new space members, the server returns\n`Membership` resources that contain the latest membership details. If new\nmembers were removed during the requested period, the event payload contains an\nempty `Membership` resource.\n\nTo call this method, you must use\n[user authentication](/workspace/chat/authenticate-authorize-chat-user). To list\nevents from a space, the authenticated user must be a member of the space.\n\nPrerequisites\n-------------\n\n\n### Node.js\n\n- A Business or Enterprise [Google Workspace](https://support.google.com/a/answer/6043576) account with access to [Google Chat](https://workspace.google.com/products/chat/).\n\n\u003c!-- --\u003e\n\n- Set up your environment:\n - [Create a Google Cloud project](/workspace/guides/create-project).\n - [Configure the OAuth consent screen](/workspace/guides/configure-oauth-consent).\n - [Enable and configure the Google Chat API](/workspace/chat/configure-chat-api) with a name, icon, and description for your Chat app.\n - Install the Node.js [Cloud Client Library](/workspace/chat/libraries?tab=nodejs#cloud-client-libraries).\n - [Create OAuth client ID credentials](/workspace/chat/authenticate-authorize-chat-user#step-2:) for a desktop application. To run the sample in this guide, save the credentials as a JSON file named `credentials.json` to your local directory.\n\n For guidance, complete the steps for setting up your environment in this [quickstart](/workspace/chat/api/guides/quickstart/nodejs\n #set-up-environment).\n- [Choose an authorization scope](/workspace/chat/authenticate-authorize#asynchronous-chat-calls) that supports user authentication.\n\n\n| The code samples in this page use the gRPC API interface with the Google Cloud client libraries. Alternatively, you can use the REST API interface. For more information about the gRPC and REST interfaces, see [Google Chat API overview](/workspace/chat/api/reference).\n\n\u003cbr /\u003e\n\nList space events\n-----------------\n\nTo list space events from a Chat space, pass the following in\nyour request:\n\n- Specify one or more authorization scopes to support each event type in your\n request. As a best practice, choose the most restrictive scope that still\n allows your app to function. To choose a scope, see the\n [Authentication and authorization overview](/workspace/chat/authenticate-authorize#space-events).\n\n- Call the\n [`ListSpaceEvents()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.ListSpaceEvents)\n method, passing the `filter` of event types to list.\n You must specify at least one event type, and you can also filter by date.\n For a list of supported event types, see the\n [`eventType` field of the `SpaceEvent` resource](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.SpaceEvent.FIELDS.string.google.chat.v1.SpaceEvent.event_type)\n reference documentation.\n\nThe following example lists events about new memberships and messages in a\nspace: \n\n### Node.js\n\nchat/client-libraries/cloud/list-space-events-user-cred.js \n[View on GitHub](https://github.com/googleworkspace/node-samples/blob/main/chat/client-libraries/cloud/list-space-events-user-cred.js) \n\n```javascript\nimport {createClientWithUserCredentials} from './authentication-utils.js';\n\n// Authorization scopes based on the event types\nconst USER_AUTH_OAUTH_SCOPES = [\n 'https://www.googleapis.com/auth/chat.memberships.readonly',\n 'https://www.googleapis.com/auth/chat.messages.readonly'\n];\n\n// This sample shows how to list space events with user credential\nasync function main() {\n // Create a client\n const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);\n\n // Initialize request argument(s)\n const request = {\n // Replace SPACE_NAME here\n parent: 'spaces/SPACE_NAME',\n // A required filter. Filters events about new memberships and messages\n filter: 'eventTypes:\"google.workspace.chat.membership.v1.created\" OR eventTypes:\"google.workspace.chat.message.v1.created\"'\n };\n\n // Make the request\n const pageResult = chatClient.listSpaceEventsAsync(request);\n\n // Handle the response. Iterating over pageResult will yield results and\n // resolve additional pages automatically.\n for await (const response of pageResult) {\n console.log(response);\n }\n}\n\nmain().catch(console.error);\n```\n\nTo run this sample, replace \u003cvar translate=\"no\"\u003eSPACE_NAME\u003c/var\u003e with the ID from\nthe space's\n[`name`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.Space.FIELDS.string.google.chat.v1.Space.name).\nYou can obtain the ID by calling the\n[`ListSpaces()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.ListSpaces)\nmethod or from the space's URL.\n\nThe Chat API returns a list of\n[paginated list of space events](/workspace/chat/api/reference/rpc/google.chat.v1#listspaceeventsresponse)\nabout new memberships and messages.\n\nRelated topics\n--------------\n\n- [Work with events from Google Chat](/workspace/chat/events-overview)\n- [Get details about a space event](/workspace/chat/get-space-event)"]]