Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In diesem Leitfaden wird beschrieben, wie Sie die Methode get() für die Ressource Attachment der Google Chat API verwenden, um Metadaten zu einem Nachrichtenanhang abzurufen. Die Antwort ist eine Instanz der Attachment-Ressource.
Wenn der Nutzer eine Nachricht an Ihre App sendet, wird in Google Chat ein MESSAGE-Interaktionsereignis gesendet.
Das von Ihrer App empfangene Interaktionsereignis enthält einen Anfragetext, der die JSON-Nutzlast für das Interaktionsereignis darstellt, einschließlich aller Anhänge. Die Daten im Anhang unterscheiden sich je nachdem, ob es sich um hochgeladene Inhalte (eine lokale Datei) oder eine in Drive gespeicherte Datei handelt. Die Media-Ressource stellt eine in Google Chat hochgeladene Datei dar, z. B. Bilder, Videos und Dokumente.
Die Attachment-Ressource stellt eine Instanz von Medien dar, also eine Datei, die an eine Nachricht angehängt ist. Die Ressource Attachment enthält die Metadaten des Anhangs, z. B. den Speicherort.
Anmeldedaten für ein Dienstkonto erstellen Wenn Sie das Beispiel in dieser Anleitung ausführen möchten, speichern Sie die Anmeldedaten als JSON-Datei mit dem Namen credentials.json in Ihrem lokalen Verzeichnis.
import{createClientWithAppCredentials}from'./authentication-utils.js';// This sample shows how to get attachment metadata with app credentialasyncfunctionmain(){// Create a clientconstchatClient=createClientWithAppCredentials();// Initialize request argument(s)constrequest={// Replace SPACE_NAME, MESSAGE_NAME, and ATTACHMENT_NAME herename:'spaces/SPACE_NAME/messages/MESSAGE_NAME/attachments/ATTACHMENT_NAME'};// Make the requestconstresponse=awaitchatClient.getAttachment(request);// Handle the responseconsole.log(response);}main().catch(console.error);
Ersetzen Sie spaces/SPACE_NAME/messages/
MESSAGE_NAME/attachments/ATTACHMENT_NAME durch den Namen des Nachrichtenanhangs, um dieses Beispiel auszuführen.
Die Chat API gibt eine Instanz von Attachment zurück, die die Metadaten zum angegebenen Nachrichtenanhang enthält.
[null,null,["Zuletzt aktualisiert: 2025-08-29 (UTC)."],[[["\u003cp\u003eThis guide explains how to retrieve metadata about a message attachment in Google Chat using the \u003ccode\u003eget()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eAttachment\u003c/code\u003e resource represents an instance of a file attached to a message and includes metadata like its location.\u003c/p\u003e\n"],["\u003cp\u003eTo get attachment metadata, you need to use the \u003ccode\u003echat.bot\u003c/code\u003e authorization scope and call the \u003ccode\u003eGetAttachment()\u003c/code\u003e method with the attachment's name.\u003c/p\u003e\n"],["\u003cp\u003ePrerequisites include a Google Workspace account, a Google Cloud project, and Node.js setup with necessary libraries and credentials.\u003c/p\u003e\n"]]],["The core actions involve retrieving metadata about a message attachment in Google Chat using the `get()` method. This requires the `chat.bot` authorization scope and calling `GetAttachment()`, providing the attachment's `name`. Before using the `get()` method, you must set up a Google Cloud project, configure the OAuth consent screen, and set up the Google Chat API. The `Attachment` resource represents the file, with metadata detailing its storage. The request triggers a response with `Attachment` details.\n"],null,["# Get metadata about a message attachment\n\nThis guide explains how to use the\n[`get()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.GetAttachment)\nmethod on the `Attachment` resource of the\nGoogle Chat API to get metadata about a message attachment. The response is an\ninstance of the\n[`Attachment` resource](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.Attachment).\n\nWhen the user sends a message to your app, Google Chat dispatches a\n[`MESSAGE` interaction event](/workspace/chat/events#message).\nThe interaction event received by your app includes a request body, which is the\nJSON payload representing the interaction event, including any attachments. The\ndata in the attachment is different depending on whether the attachment is\nuploaded content (a local file) or is a file stored on Drive. The\n[`Media` resource](/workspace/chat/api/reference/rest/v1/media)\nrepresents a file uploaded to Google Chat, like images, videos, and documents.\nThe\n[`Attachment` resource](/workspace/chat/api/reference/rest/v1/spaces.messages.attachments)\nrepresents an instance of media---a file---attached to a message. The `Attachment`\nresource includes the metadata about the attachment, such as\nwhere it's saved.\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 service account credentials](/workspace/chat/authenticate-authorize-chat-app#create-service-account). To run the sample in this guide, save the credentials as a JSON file named `credentials.json` to your local directory.\n- [Choose an authorization scope](/workspace/chat/authenticate-authorize#asynchronous-chat-calls) that supports app 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\nGet a message attachment\n------------------------\n\nTo asynchronously get metadata about a message attachment in Google Chat, pass\nthe following in your request:\n\n- Specify the `chat.bot` authorization scope.\n- Call the [`GetAttachment()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.GetAttachment) method, passing the `name` of the message attachment.\n\nHere's how to get metadata about a message attachment: \n\n### Node.js\n\nchat/client-libraries/cloud/get-attachment-app-cred.js \n[View on GitHub](https://github.com/googleworkspace/node-samples/blob/main/chat/client-libraries/cloud/get-attachment-app-cred.js) \n\n```javascript\nimport {createClientWithAppCredentials} from './authentication-utils.js';\n\n// This sample shows how to get attachment metadata with app credential\nasync function main() {\n // Create a client\n const chatClient = createClientWithAppCredentials();\n\n // Initialize request argument(s)\n const request = {\n // Replace SPACE_NAME, MESSAGE_NAME, and ATTACHMENT_NAME here\n name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME/attachments/ATTACHMENT_NAME'\n };\n\n // Make the request\n const response = await chatClient.getAttachment(request);\n\n // Handle the response\n console.log(response);\n}\n\nmain().catch(console.error);\n```\n\nTo run this sample, replace `spaces/`\u003cvar translate=\"no\"\u003eSPACE_NAME\u003c/var\u003e`/messages/\n`\u003cvar translate=\"no\"\u003eMESSAGE_NAME\u003c/var\u003e`/attachments/`\u003cvar translate=\"no\"\u003eATTACHMENT_NAME\u003c/var\u003e with the\nmessage attachment name.\n\nThe Chat API returns an instance of\n[`Attachment`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.Attachment)\nthat details the metadata about the specified message attachment.\n\nRelated topics\n--------------\n\n- [Upload media as a file attachment](/workspace/chat/upload-media-attachments)\n- [Download media as a file attachment](/workspace/chat/download-media-attachments)"]]