Phầ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 API Google Workspace.
Các hướng dẫn nhanh về Google Workspace sử dụng thư viện ứng dụng API để xử lý một số thông tin chi tiết về quy trình xác thực và uỷ quyền. Bạn nên sử dụng thư viện ứng dụng cho các ứng dụng của riêng mình. Phần bắt đầu nhanh này sử dụng phương pháp xác thực đơn giản phù hợp với môi trường thử nghiệm. Đối với môi trường phát hành công khai, bạn nên tìm hiểu về quy trình xác thực và uỷ quyền trước khi chọn thông tin xác thực truy cập phù hợp với ứng dụng của mình.
/** * Print the display name if available for 10 connections. */functionlistConnectionNames(){try{/** * List the 10 connections/contacts of user * @see https://developers.google.com/people/api/rest/v1/people.connections/list */constconnections=People.People.Connections.list('people/me',{pageSize:10,personFields:'names,emailAddresses'// use other query parameter here if needed.});connections.connections.forEach((person)=>{// if contacts/connections is available, print the name of person.if(person.names && person.names.length===0){console.log('Nodisplaynamefoundforconnection.');return;}console.log(person.names[0].displayName);});}catch(err){// TODO (developer) - Handle exception from People APIconsole.log('Failedwitherror%s',err.message);}}
Nhấp vào biểu tượng Lưu .
Nhấp vào Untitled project (Dự án chưa có tên), nhập Quickstart (Bắt đầu nhanh) rồi nhấp vào Rename (Đổi tên).
Định cấu hình tập lệnh
Bật People API
Mở dự án Apps Script.
Nhấp vào biểu tượng Trình chỉnh sửacode.
Bên cạnh Dịch vụ, hãy nhấp vào biểu tượng Thêm dịch vụ
add .
Chọn API People rồi nhấp vào Thêm.
Chạy mẫu
Trong trình chỉnh sửa Apps Script, hãy nhấp vào Run (Chạy).
Trong lần đầu chạy mẫu, bạn sẽ được nhắc uỷ quyền truy cập:
Nhấp vào Xem lại quyền.
Chọn một tài khoản.
Nhấp vào Cho phép.
Nhật ký thực thi của tập lệnh sẽ xuất hiện ở cuối cửa sổ.
[null,null,["Cập nhật lần gần đây nhất: 2024-12-21 UTC."],[[["This quickstart demonstrates how to use Google Apps Script to access the People API and retrieve a list of connections."],["Before starting, ensure you have a Google Workspace domain with API access enabled and a Google Account with administrator privileges."],["The guide walks you through creating, configuring, and running a script that prints the display names of your connections."],["You'll need to enable the People API in your Apps Script project and authorize the script to access your Google account."],["For production environments, Google recommends reviewing authentication and authorization best practices."]]],["This document guides users to create and run a Google Apps Script that interacts with the People API. It involves configuring a new script on `script.google.com`, replacing the script editor's content with provided code, saving, and renaming the project. Users must enable the People API as a service within the script and authorize access during the initial run. The script then lists the display names of 10 connections. Production environments need appropriate authentication credentials.\n"]]