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.

Prerequisites

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 script.google.com/create.
  2. Replace the contents of the script editor with the following code:

drive/activity-v2/quickstart.gs
/**
 * 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 = activity.actors.map(getActorInfo);
      var targets = activity.targets.map(getTargetInfo);
      Logger.log(
          '%s: %s, %s, %s', time, truncated(actors), action,
          truncated(targets));
    }
  } 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 = target.drive.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';
}

  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

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

Troubleshooting

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.

The OAuth consent screen that is presented to the user may show the warning "This app isn't verified" if it is requesting scopes that provide access to sensitive user data. These applications must eventually 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).

Send feedback about...

Google Drive Activity API
Need help? Visit our support page.