Drive Activity API v1 has reached the end of its deprecation period and is shutting down as announced in December 2018. Migrate to API v2 as soon as possible to avoid disruptions to your application.

Google Apps Script Quickstart

Complete the steps described in the rest of this page to create a simple Google Apps Script that makes requests to the Google Drive Activity API.


To run this quickstart, you need the following prerequisites:

  • A Google account
  • Access to Google Drive

Step 1: Create the script

  1. Create a new script by going to
  2. Replace the contents of the script editor with the following code:

 * Lists activity for a Drive user.
function listDriveActivity() {
  var request = {pageSize: 10};
  var response = DriveActivity.Activity.query(request);
  var activities = response.activities;
  if (activities && activities.length > 0) {
    Logger.log('Recent activity:');
    for (var i = 0; i < activities.length; i++) {
      var activity = activities[i];
      var time = getTimeInfo(activity);
      var action = getActionInfo(activity.primaryActionDetail);
      var actors =;
      var targets =;
          '%s: %s, %s, %s', time, truncated(actors), action,
  } else {
    Logger.log('No activity.');

/** Returns a string representation of the first elements in a list. */
function truncated(array, opt_limit) {
  var limit = opt_limit || 2;
  var contents = array.slice(0, limit).join(', ');
  var more = array.length > limit ? ', ...' : '';
  return '[' + contents + more + ']';

/** Returns the name of a set property in an object, or else "unknown". */
function getOneOf(object) {
  for (var key in object) {
    return key;
  return 'unknown';

/** Returns a time associated with an activity. */
function getTimeInfo(activity) {
  if ('timestamp' in activity) {
    return activity.timestamp;
  if ('timeRange' in activity) {
    return activity.timeRange.endTime;
  return 'unknown';

/** Returns the type of action. */
function getActionInfo(actionDetail) {
  return getOneOf(actionDetail);

/** Returns user information, or the type of user if not a known user. */
function getUserInfo(user) {
  if ('knownUser' in user) {
    var knownUser = user.knownUser;
    var isMe = knownUser.isCurrentUser || false;
    return isMe ? 'people/me' : knownUser.personName;
  return getOneOf(user);

/** Returns actor information, or the type of actor if not a user. */
function getActorInfo(actor) {
  if ('user' in actor) {
    return getUserInfo(actor.user)
  return getOneOf(actor);

/** Returns the type of a target and an associated title. */
function getTargetInfo(target) {
  if ('driveItem' in target) {
    var title = target.driveItem.title || 'unknown';
    return 'driveItem:"' + title + '"';
  if ('drive' in target) {
    var title = || 'unknown';
    return 'drive:"' + title + '"';
  if ('fileComment' in target) {
    var parent = target.fileComment.parent || {};
    var title = parent.title || 'unknown';
    return 'fileComment:"' + title + '"';
  return getOneOf(target) + ':unknown';

New editor

  1. Click Save .
  2. Click Untitled project at the top left, type Quickstart, and click Rename.

Legacy editor

  1. Click File > Save, name your project “Quickstart”, and click OK.

Step 2: Turn on the Google Drive Activity API

Enable the Drive Activity API advanced service in your script.

Step 3: Run the sample

New editor

In the Apps Script editor, click Run.

The first time you run the sample, it will prompt you to authorize access:

  1. Click Review permissions.
  2. Choose an account.
  3. Click Allow.

The script's execution log appears at the bottom of the window.

Legacy editor

In the Apps Script editor, click Run > listDriveActivity.

The first time you run the sample, it will prompt you to authorize access:

  1. Click the Continue button.
  2. Click the Accept button.

To view the script's output, click View > Logs.

Further reading


This section describes some common issues that you may encounter while attempting to run this quickstart and suggests possible solutions.

ReferenceError: "[API NAME]" is not defined

This error occurs when the API hasn't been toggled on in the Apps Script code editor. Revisit Step 2.b and ensure the corresponding toggle is set to on.

This app isn't verified

If the OAuth consent screen displays the warning "This app isn't verified," your app is requesting scopes that provide access to sensitive user data. If your application uses sensitive scopes, your your app must go through the verification process to remove that warning and other limitations. During the development phase you can continue past this warning by clicking Advanced > Go to {Project Name} (unsafe).