Google Apps 脚本快速入门

快速入门介绍了如何设置和运行调用 Google Workspace API。

Google Workspace 快速入门使用 API 客户端库来处理一些 身份验证和授权流程的详细信息。我们建议 您需要为自己的应用使用客户端库本快速入门使用 适用于测试的简化身份验证方法 环境对于生产环境,我们建议您了解 身份验证和授权 早于 选择访问凭据 适合您应用的广告格式

创建 Google Apps 脚本 向 Google Drive Activity API 发出请求。

目标

  • 配置环境。
  • 创建并配置脚本。
  • 运行脚本。

前提条件

  • Google 账号
  • 访问 Google 云端硬盘

创建脚本

  1. 前往 script.google.com/create 创建新脚本。
  2. 将脚本编辑器的内容替换为以下代码:

drive/activity-v2/quickstart.gs
/**
 * Lists 10 activity for a Drive user.
 * @see https://developers.google.com/drive/activity/v2/reference/rest/v2/activity/query
 */
function listDriveActivity() {
  const request = {
    pageSize: 10
    // Use other parameter here if needed.
  };
  try {
    // Activity.query method is used Query past activity in Google Drive.
    const response = DriveActivity.Activity.query(request);
    const activities = response.activities;
    if (!activities || activities.length === 0) {
      console.log('No activity.');
      return;
    }
    console.log('Recent activity:');
    for (const activity of activities) {
      // get time information of activity.
      const time = getTimeInfo(activity);
      // get the action details/information
      const action = getActionInfo(activity.primaryActionDetail);
      // get the actor's details of activity
      const actors = activity.actors.map(getActorInfo);
      // get target information of activity.
      const targets = 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 API
    console.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".
 */
function getOneOf(object) {
  for (const key in object) {
    return key;
  }
  return 'unknown';
}

/**
 * @param {object} activity Activity object.
 * @return {string} 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';
}

/**
 * @param {object} actionDetail The primary action details of the activity.
 * @return {string} Returns the type of action.
 */
function getActionInfo(actionDetail) {
  return getOneOf(actionDetail);
}

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

/**
 * @param {object} actor The Actor object.
 * @return {string} 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);
}

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

  1. 点击“保存”
  2. 点击未命名项目,输入 快速入门,然后点击重命名

配置脚本

启用 Google Drive Activity API

  1. 打开 Apps 脚本项目。
  1. 点击 Editor
  2. 服务旁边,点击“添加服务”
  3. 选择 Drive Activity API 然后点击添加

运行示例

在 Apps 脚本编辑器中,点击运行

首次运行该示例时,它会提示您授予访问权限:

  1. 点击查看权限
  2. 选择账号。
  3. 点击允许

脚本的执行日志显示在窗口底部。

<ph type="x-smartling-placeholder"> <ph type="x-smartling-placeholder">

后续步骤