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.
Tạo một Google Apps Script để gửi yêu cầu đến API Hoạt động của Google Drive.
/** * Lists 10 activity for a Drive user. * @see https://developers.google.com/drive/activity/v2/reference/rest/v2/activity/query */functionlistDriveActivity(){constrequest={pageSize:10// Use other parameter here if needed.};try{// Activity.query method is used Query past activity in Google Drive.constresponse=DriveActivity.Activity.query(request);constactivities=response.activities;if(!activities||activities.length===0){console.log('Noactivity.');return;}console.log('Recentactivity:');for(constactivityofactivities){// get time information of activity.consttime=getTimeInfo(activity);// get the action details/informationconstaction=getActionInfo(activity.primaryActionDetail);// get the actor's details of activityconstactors=activity.actors.map(getActorInfo);// get target information of activity.consttargets=activity.targets.map(getTargetInfo);// print the time,actor,action and targets of drive activity.console.log('%s:%s,%s,%s',time,actors,action,targets);}}catch(err){// TODO (developer) - Handle error from drive activity APIconsole.log('Failedwithanerror%s',err.message);}}/** * @param {object} object * @return {string} Returns the name of a set property in an object, or else "unknown". */functiongetOneOf(object){for(constkeyinobject){returnkey;}return'unknown';}/** * @param {object} activity Activity object. * @return {string} Returns a time associated with an activity. */functiongetTimeInfo(activity){if('timestamp'inactivity){returnactivity.timestamp;}if('timeRange'inactivity){returnactivity.timeRange.endTime;}return'unknown';}/** * @param {object} actionDetail The primary action details of the activity. * @return {string} Returns the type of action. */functiongetActionInfo(actionDetail){returngetOneOf(actionDetail);}/** * @param {object} user The User object. * @return {string} Returns user information, or the type of user if not a known user. */functiongetUserInfo(user){if('knownUser'inuser){constknownUser=user.knownUser;constisMe=knownUser.isCurrentUser||false;returnisMe?'people/me':knownUser.personName;}returngetOneOf(user);}/** * @param {object} actor The Actor object. * @return {string} Returns actor information, or the type of actor if not a user. */functiongetActorInfo(actor){if('user'inactor){returngetUserInfo(actor.user);}returngetOneOf(actor);}/** * @param {object} target The Target object. * @return {string} Returns the type of a target and an associated title. */functiongetTargetInfo(target){if('driveItem'intarget){consttitle=target.driveItem.title||'unknown';return'driveItem:"' + title + '"';}if('drive'intarget){consttitle=target.drive.title||'unknown';return'drive:"' + title + '"';}if('fileComment'intarget){constparent=target.fileComment.parent||{};consttitle=parent.title||'unknown';return'fileComment:"' + title + '"';}returngetOneOf(target)+':unknown';}
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 API Hoạt động trong Google Drive
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
Drive Activity API
và 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).
Lần đầu tiên 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: 2025-01-30 UTC."],[[["This quickstart demonstrates how to set up and run a Google Apps Script that utilizes the Google Drive Activity API to retrieve and display a user's recent Drive activity."],["To use this quickstart, you need a Google Account with access to Google Drive and will need to enable the Drive Activity API for your script."],["The script provided in the quickstart retrieves the 10 most recent activities in your Google Drive, including details like time, actor, action, and targets."],["After authorizing the script to access your Google Drive data, you can execute it directly from the Apps Script editor to view the activity log in the execution output."],["For production environments, it is recommended to further explore Google's authentication and authorization documentation before implementing the provided simplified approach."]]],["This guide demonstrates how to set up and run a Google Apps Script to interact with the Google Drive Activity API. The process involves creating a new script, replacing its contents with provided code, and enabling the Drive Activity API in the script's services. The script then queries and displays the most recent ten activities in Google Drive. Users are prompted to authorize access, and the execution log will output the recent activities or indicate if there is an error.\n"]]