Google Apps Script Quickstart

Complete the steps described in the rest of this page, and in about five minutes you'll have a simple Google Apps Script that makes requests to the Google Drive Activity API.

Prerequisites

To run this quickstart, you'll need:

  • A G Suite account with Google Drive enabled.
  • 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/quickstart.gs
    /**
     * Lists activity for a Drive user.
     */
    function listActivity() {
      var optionalArgs = {
        'source': 'drive.google.com',
        'drive.ancestorId': 'root',
        'pageSize': 10
      };
      var response = AppsActivity.Activities.list(optionalArgs);
      var activities = response.activities;
      if (activities && activities.length > 0) {
        Logger.log('Recent activity:');
        for (i = 0; i < activities.length; i++) {
          var activity = activities[i];
          var event = activity.combinedEvent;
          var user = event.user;
          var target = event.target;
          if (user == null || target == null) {
            continue;
          } else {
            var time = new Date(Number(event.eventTimeMillis));
            Logger.log('%s: %s, %s, %s (%s)', time, user.name,
                  event.primaryEventType, target.name, target.mimeType);
          }
        }
      } else {
        Logger.log('No recent activity');
      }
    }

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

    Step 2: Turn on the Google Drive Activity API

    1. In the Apps Script editor, click Resources > Advanced Google Services.
    2. Locate Drive Activity API in the dialog and click the corresponding toggle, setting it to on.
    3. Click the Google API Console link.
    4. Enter "Drive Activity API" into the search box and click on the corresponding entry in the results.
    5. Click the Enable API button.
    6. Return to the Apps Script editor and click the OK button on the Advanced Google Services dialog.

    Step 3: Run the sample

    In the Apps Script editor, click Run > listActivity.

    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

    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.

    Send feedback about...

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