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);
如需运行此示例,请将 SPACE_NAME 替换为空间 name 中的 ID。您可以通过调用 ListSpaces() 方法或从空间的网址中获取 ID。
[null,null,["最后更新时间 (UTC):2025-08-04。"],[[["\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)"]]