Google Apps Script quickstart Stay organized with collections Save and categorize content based on your preferences.
Create aGoogle Apps Scriptthat makes requests to the Google Drive Activity API.
Quickstarts explain how to set up and run an app that calls aGoogle Workspace API. This quickstart uses asimplified authentication approach that is appropriate for a testingenvironment. For a production environment, we recommend learning aboutauthentication and authorizationbeforechoosing the access credentialsthat are appropriate for your app.
In Apps Script, Google Workspacequickstarts useAdvanced Google services to callGoogle Workspace APIs and handle some details of the authenticationand authorization flow.
Objectives
- Configure the environment.
- Create and configure the script.
- Run the script.
Prerequisites
- A Google Account
- Access to Google Drive
Create the script
- Create a new script in the Apps Script editor by going toscript.google.com/create.
- Replace the contents of the script editor with the following code:
/** * 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("No activity.");return;}console.log("Recent activity:");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("Failed with an error %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}"`;}return`${getOneOf(target)}:unknown`;}
- Click Save
.
- ClickUntitled project, typeQuickstart, and clickRename.
Configure the script
Enable the Google Drive Activity API
Open the Apps Script project.
- ClickEditor.
- Next toServices, click Add a service .
- Select Drive Activity APIand clickAdd.
Run the sample
In the Apps Script editor, clickRun.
The first time you run the sample, it prompts you to authorize access:
- ClickReview permissions.
- Choose an account.
- ClickAllow.
The script's execution log appears at the bottom of the window.
Next steps
- Google Apps Script Advanced Services documentation
- Try the Google Workspace APIs in the APIs explorer
- Troubleshoot authentication and authorization issues
- Drive Activity API reference documentation
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.