Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Panduan ini menjelaskan cara menggunakan metode
list()
pada resource SpaceEvent Google Chat API untuk mencantumkan perubahan pada
resource dalam ruang.
Resource
SpaceEvent
mewakili perubahan pada ruang target, termasuk resource turunan ruang
seperti pesan, reaksi, dan keanggotaan. Untuk mengetahui informasi selengkapnya tentang daftar jenis peristiwa dan payload peristiwa yang didukung, lihat kolom eventType dan payload dalam dokumentasi referensi resource SpaceEvent.
Anda dapat mencantumkan acara hingga 28 hari sebelum waktu permintaan. Server menampilkan peristiwa yang berisi versi terbaru dari resource yang terpengaruh.
Misalnya, jika Anda mencantumkan peristiwa tentang anggota ruang baru, server akan menampilkan
Membership resource yang berisi detail keanggotaan terbaru. Jika anggota baru dihapus selama periode yang diminta, payload peristiwa berisi resource Membership kosong.
Untuk memanggil metode ini, Anda harus menggunakan
autentikasi pengguna. Untuk mencantumkan
acara dari ruang, pengguna terautentikasi harus menjadi anggota ruang tersebut.
Buat kredensial client ID OAuth untuk aplikasi desktop. Untuk menjalankan contoh dalam panduan ini, simpan kredensial sebagai file JSON bernama credentials.json ke direktori lokal Anda.
Untuk mendapatkan panduan, selesaikan langkah-langkah penyiapan lingkungan Anda dalam
panduan memulai cepat ini.
Untuk mencantumkan peristiwa ruang dari ruang Chat, teruskan hal berikut dalam
permintaan Anda:
Tentukan satu atau beberapa cakupan otorisasi untuk mendukung setiap jenis peristiwa dalam permintaan Anda. Sebagai praktik terbaik, pilih cakupan paling ketat yang masih
memungkinkan aplikasi Anda berfungsi. Untuk memilih cakupan, lihat
Ringkasan autentikasi dan otorisasi.
Panggil metode
ListSpaceEvents(), dengan meneruskan filter jenis peristiwa yang akan dicantumkan.
Anda harus menentukan setidaknya satu jenis peristiwa, dan Anda juga dapat memfilter menurut tanggal.
Untuk mengetahui daftar jenis peristiwa yang didukung, lihat kolom
eventType dari dokumentasi referensi SpaceEvent resource.
Contoh berikut mencantumkan peristiwa tentang pesan dan langganan baru dalam ruang:
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);
Untuk menjalankan contoh ini, ganti SPACE_NAME dengan ID dari
name ruang.
Anda bisa mendapatkan ID dengan memanggil metode
ListSpaces()
atau dari URL ruang.
[null,null,["Terakhir diperbarui pada 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)"]]