Hướng dẫn bắt đầu nhanh giải thích cách thiết lập và chạy một ứng dụng gọi một API Google Workspace. Hướng dẫn bắt đầu nhanh này sử dụng một phương pháp xác thực đơn giản phù hợp với môi trường kiểm thử. Đối với môi trường phát hành công khai, bạn nên tìm hiểu về xác thực và uỷ quyền trước khi chọn thông tin đăng nhập để truy cập phù hợp với ứng dụng của bạn.
Trong Apps Script, các hướng dẫn nhanh về Google Workspace sử dụng Dịch vụ nâng cao của Google để gọi API Google Workspace và xử lý một số thông tin chi tiết về quy trình xác thực và uỷ quyền.
Nếu đang sử dụng một dự án Google Cloud mới để hoàn tất hướng dẫn nhanh này, bạn cần định cấu hình dự án đó và thêm chính mình làm người dùng thử nghiệm. Nếu bạn đã hoàn tất các bước này cho dự án trên Cloud, hãy chuyển sang phần tiếp theo.
Mở dự án trên Cloud trong Google Cloud Console
Nếu chưa mở, hãy mở dự án trên Cloud mà bạn dự định dùng cho mẫu này:
Trong Google Cloud Console, hãy chuyển đến trang Chọn một dự án.
Chọn dự án trong Google Cloud mà bạn muốn sử dụng. Hoặc nhấp vào Tạo dự án rồi làm theo hướng dẫn trên màn hình. Nếu tạo một dự án trên Google Cloud, bạn có thể cần bật tính năng thanh toán cho dự án đó.
Bật Chat API
Trước khi sử dụng API của Google, bạn cần bật các API đó trong một dự án Google Cloud. Bạn có thể bật một hoặc nhiều API trong một dự án Google Cloud.
Trong Google Cloud Console, hãy bật Google Chat API.
Nếu đã định cấu hình , bạn có thể định cấu hình các chế độ cài đặt sau đây cho Màn hình đồng ý OAuth trong phần Thương hiệu, Đối tượng và Quyền truy cập vào dữ liệu. Nếu bạn thấy thông báo chưa được định cấu hình, hãy nhấp vào Bắt đầu:
Trong mục Thông tin về ứng dụng, trong phần Tên ứng dụng, hãy nhập tên cho ứng dụng.
Trong phần Email hỗ trợ người dùng, hãy chọn một địa chỉ email hỗ trợ mà người dùng có thể liên hệ với bạn nếu họ có thắc mắc về sự đồng ý của mình.
Nhấp vào Tiếp theo.
Trong phần Đối tượng người xem, hãy chọn Nội bộ.
Nhấp vào Tiếp theo.
Trong phần Thông tin liên hệ, hãy nhập Địa chỉ email để bạn có thể nhận thông báo về mọi thay đổi đối với dự án của mình.
Hiện tại, bạn có thể bỏ qua bước thêm phạm vi. Trong tương lai, khi tạo một ứng dụng để sử dụng bên ngoài tổ chức Google Workspace, bạn phải thay đổi Loại người dùng thành Bên ngoài. Sau đó, hãy thêm các phạm vi uỷ quyền mà ứng dụng của bạn yêu cầu. Để tìm hiểu thêm, hãy xem hướng dẫn đầy đủ về cách Định cấu hình sự đồng ý của OAuth.
Định cấu hình ứng dụng Google Chat
Để gọi Google Chat API, bạn phải định cấu hình một ứng dụng Google Chat. Đối với mọi yêu cầu ghi, Google Chat sẽ phân bổ ứng dụng Google Chat trong giao diện người dùng bằng cách sử dụng thông tin sau.
Trong Google Cloud Console, hãy chuyển đến trang Configuration (Cấu hình) của Chat API:
Trong phần Thông tin về ứng dụng, hãy nhập những thông tin sau:
Trong trường Tên ứng dụng, hãy nhập Chat API quickstart app.
Trong trường URL hình đại diện, hãy nhập https://developers.google.com/chat/images/quickstart-app-avatar.png.
Trong trường Description (Nội dung mô tả), hãy nhập Quickstart for calling the Chat API.
Trong phần Tính năng tương tác, hãy nhấp vào nút bật/tắt Bật tính năng tương tác để chuyển sang trạng thái tắt nhằm vô hiệu hoá các tính năng tương tác cho ứng dụng Chat.
/** * This quickstart sample shows how to list spaces with user credential * * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces.readonly' * referenced in the manifest file (appsscript.json). */functionlistSpaces(){// Initialize request argument(s)// Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)constfilter='space_type = "SPACE"';// Iterate through the response pages using page tokensletresponsePage;letpageToken=null;do{// Request response pagesresponsePage=Chat.Spaces.list({filter:filter,pageToken:pageToken});// Handle response pagesif(responsePage.spaces){responsePage.spaces.forEach((space)=>console.log(space));}// Update the page token to the next onepageToken=responsePage.nextPageToken;}while(pageToken);}
Nhấp vào biểu tượng Lưu .
Nhấp vào Dự án chưa có tên, nhập Quickstart (Bắt đầu nhanh) rồi nhấp vào Đổi tên.
Định cấu hình tập lệnh
Sao chép số dự án trên Cloud
Trong Bảng điều khiển Google Cloud, hãy chuyển đến phần Trình đơn menu>IAM và Quản trị>Cài đặt.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-04 UTC."],[[["\u003cp\u003eThis quickstart demonstrates how to set up and run a Google Apps Script that interacts with the Google Chat API to list spaces.\u003c/p\u003e\n"],["\u003cp\u003eBefore running the script, you need to configure your Google Cloud project, enable the Chat API, and set up OAuth consent.\u003c/p\u003e\n"],["\u003cp\u003eThe script utilizes the Google Chat API client libraries for authentication and authorization and provides a simplified approach suitable for testing environments.\u003c/p\u003e\n"],["\u003cp\u003eYou need a Google Workspace account and a Google Cloud project to complete the steps outlined in the guide.\u003c/p\u003e\n"],["\u003cp\u003eTo run the script, you will need to copy your Cloud project number, link it to the Apps Script project, enable the Google Chat API, and authorize access.\u003c/p\u003e\n"]]],["This document outlines how to create and run a Google Apps Script to interact with the Google Chat API. Key actions include: configuring a Google Cloud project (enabling the Chat API, setting up OAuth consent, and configuring a Chat app), creating a new script, linking the script to the Cloud project, configuring the script by enabling the Chat API in its manifest file, and running the script. This allows the listing of spaces and other API interaction.\n"],null,["Create a\n[Google Apps Script](/apps-script/overview)\nthat makes requests to the Google Chat API.\n\nQuickstarts explain how to set up and run an app that calls a\nGoogle Workspace API. This quickstart uses a\nsimplified authentication approach that is appropriate for a testing\nenvironment. For a production environment, we recommend learning about\n[authentication and authorization](/workspace/guides/auth-overview)\nbefore\n[choosing the access credentials](/workspace/guides/create-credentials#choose_the_access_credential_that_is_right_for_you)\nthat are appropriate for your app.\n\nIn Apps Script, Google Workspace\nquickstarts use\n[Advanced Google services](/apps-script/guides/services/advanced) to call\nGoogle Workspace APIs and handle some details of the authentication\nand authorization flow.\n\nObjectives\n\n- Configure the environment.\n- Create and configure the script.\n- Run the script.\n\nPrerequisites\n\n- [A Google Cloud project](/workspace/guides/create-project).\n\n\n- A Business or Enterprise [Google Workspace](//support.google.com/a/answer/6043576) account with access to [Google Chat](//workspace.google.com/products/chat/).\n\n\u003cbr /\u003e\n\nConfigure your Cloud project\n\nIf you're using a new Google Cloud project to complete this quickstart, you need to\nconfigure it and add yourself as a test user. If you've already\ncompleted these steps for your Cloud project, skip to the next section.\n\nOpen your Cloud project in the Google Cloud console\n\nIf it's not open already, open the Cloud project that you intend to use\nfor this sample:\n\n1. In the Google Cloud console, go to the **Select a project** page.\n\n [Select a Cloud project](https://console.cloud.google.com/projectselector2)\n2. Select the Google Cloud project you want to use. Or, click **Create project** and follow the on-screen instructions. If you create a Google Cloud project, you might need to [turn on billing for the project](https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_a_project).\n\nTurn on the Chat API Before using Google APIs, you need to turn them on in a Google Cloud project. You can turn on one or more APIs in a single Google Cloud project.\n\n- In the Google Cloud console, enable the Google Chat API.\n\n [Enable the API](https://console.cloud.google.com/flows/enableapi?apiid=chat.googleapis.com)\n\nConfigure the OAuth consent screen\n\n1. In the Google Cloud console, go to Menu menu \\\u003e **Google Auth platform** \\\u003e **Branding** .\n\n [Go to Branding](https://console.cloud.google.com/auth/branding)\n2. If you have already configured the Google Auth platform, you can configure the following OAuth Consent Screen settings in [Branding](https://console.cloud.google.com/auth/branding), [Audience](https://console.cloud.google.com/auth/audience), and [Data Access](https://console.cloud.google.com/auth/scopes). If you see a message that says **Google Auth platform not configured yet** , click **Get Started**:\n 1. Under **App Information** , in **App name**, enter a name for the app.\n 2. In **User support email**, choose a support email address where users can contact you if they have questions about their consent.\n 3. Click **Next**.\n 4. Under **Audience** , select **Internal**.\n 5. Click **Next**.\n 6. Under **Contact Information** , enter an **Email address** where you can be notified about any changes to your project.\n 7. Click **Next**.\n 8. Under **Finish** , review the [Google API Services User Data Policy](https://developers.google.com/terms/api-services-user-data-policy) and if you agree, select **I agree to the Google API Services: User Data Policy**.\n 9. Click **Continue**.\n 10. Click **Create**.\n3. For now, you can skip adding scopes. In the future, when you create an app for use outside of your Google Workspace organization, you must change the **User type** to **External** . Then add the authorization scopes that your app requires. To learn more, see the full [Configure OAuth consent](/workspace/guides/configure-oauth-consent) guide.\n\nConfigure the Google Chat app\n\nTo call the Google Chat API, you must configure a\nGoogle Chat app. For any write requests, Google Chat\nattributes the Google Chat app in the UI using\nthe following information.\n\n1. In the Google Cloud console, go to the Chat API **Configuration** page:\n\n [Go to Chat API Configuration page](https://console.cloud.google.com/apis/api/chat.googleapis.com/hangouts-chat)\n2. Under **Application info**, enter the following information:\n\n 1. In the **App name** field, enter `Chat API quickstart app`.\n 2. In the **Avatar URL** field, enter `https://developers.google.com/chat/images/quickstart-app-avatar.png`.\n 3. In the **Description** field, enter `Quickstart for calling the Chat API`.\n3. Under **Interactive features** , click the **Enable interactive features**\n toggle to the off position to disable interactive features for the\n Chat app.\n\n4. Click **Save**.\n\nCreate the script\n\n1. Create a new script by going to [script.google.com/create](https://script.google.com/create).\n2. Replace the contents of the script editor with the following code:\n\n\nchat/quickstart/Code.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/chat/quickstart/Code.gs) \n\n```gosu\n/**\n * This quickstart sample shows how to list spaces with user credential\n *\n * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.spaces.readonly'\n * referenced in the manifest file (appsscript.json).\n */\nfunction listSpaces() {\n // Initialize request argument(s)\n // Filter spaces by space type (SPACE or GROUP_CHAT or DIRECT_MESSAGE)\n const filter = 'space_type = \"SPACE\"';\n\n // Iterate through the response pages using page tokens\n let responsePage;\n let pageToken = null;\n do {\n // Request response pages\n responsePage = Chat.Spaces.list({\n filter: filter,\n pageToken: pageToken\n });\n // Handle response pages\n if (responsePage.spaces) {\n responsePage.spaces.forEach((space) =\u003e console.log(space));\n }\n // Update the page token to the next one\n pageToken = responsePage.nextPageToken;\n } while (pageToken);\n}\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n3. Click Save .\n4. Click **Untitled project** , type **Quickstart** , and click **Rename**.\n\n\u003cbr /\u003e\n\nConfigure the script\n\nCopy the Cloud project number\n\n1. In the Google Cloud console, go to Menu menu \\\u003e **IAM \\& Admin** \\\u003e **Settings** .\n\n [Go to IAM \\& Admin Settings](https://console.cloud.google.com/iam-admin/settings)\n2. In the **Project number** field, copy the value.\n\nLink your Google Cloud project\n\n1. Open the Apps Script project.\n2. Click **Project Settings** settings.\n3. Under **Google Cloud Platform (GCP) Project** , click **Change project**.\n4. In **GCP project number**, paste the Google Cloud project number.\n5. Click **Set project**.\n\nEnable the Google Chat API\n\nOpen the Apps Script project.\n\n1. Click **Project Settings** settings.\n2. Under **General settings** , enable **Show \"appsscript.json\" manifest file\n in editor**.\n3. Click **Editor** code, select the file `appscript.json`, and replace the contents with the following code:\n\n\nchat/quickstart/appsscript.json \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/chat/quickstart/appsscript.json) \n\n```json\n{\n \"timeZone\": \"America/New_York\",\n \"exceptionLogging\": \"STACKDRIVER\",\n \"runtimeVersion\": \"V8\",\n \"oauthScopes\": [\n \"https://www.googleapis.com/auth/chat.spaces.readonly\"\n ],\n \"chat\": {},\n \"dependencies\": {\n \"enabledAdvancedServices\": [{\n \"userSymbol\": \"Chat\",\n \"version\": \"v1\",\n \"serviceId\": \"chat\"\n }]\n }\n}\n```\n\n\u003cbr /\u003e\n\nRun the sample\n\nIn the Apps Script editor, click **Run**.\n\nThe first time you run the sample, it prompts you to authorize access:\n\n1. Click **Review permissions**.\n2. Choose an account.\n3. Click **Allow**.\n\nThe script's execution log appears at the bottom of the window. \ndone It worked! **Great!** Check out the further reading section below to learn more.\nwarning There was a problem **Bummer** , [let us know what went wrong](#). Check out our [troubleshooting](#troubleshooting) section below for some common errors and solutions. If you have found a bug in the code, [report the issue on GitHub](https://github.com/googleworkspace/apps-script-samples/issues) or submit a pull request.\n\nNext steps\n\n- [Google Apps Script Advanced Services documentation](/apps-script/guides/services/advanced)"]]