Stay organized with collections
Save and categorize content based on your preferences.
Quickstarts explain how to set up and run an app that calls a
Google Workspace API.
Google Workspace quickstarts use the API client libraries to handle some
details of the authentication and authorization flow. We recommend that
you use the client libraries for your own apps. This quickstart uses a
simplified authentication approach that is appropriate for a testing
environment. For a production environment, we recommend learning about
authentication and authorization
before
choosing the access credentials
that are appropriate for your app.
Create a
Google Apps Script
that makes requests to the Google Drive Activity API.
/** * 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';}
Click Save .
Click Untitled project, type
Quickstart, and click Rename.
Configure the script
Enable the Google Drive Activity API
Open the Apps Script project.
Click Editorcode.
Next to Services, click Add a service
add .
Select
Drive Activity API
and click Add.
Run the sample
In the Apps Script editor, click Run.
The first time you run the sample, it prompts you to authorize access:
Click Review permissions.
Choose an account.
Click Allow.
The script's execution log appears at the bottom of the window.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-12-19 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."]]],[]]