شروع سریع اسکریپت Google Apps

یک اسکریپت Google Apps ایجاد کنید که درخواست‌هایی را به API فعالیت Google Drive ارسال کند.

آموزش‌های سریع، نحوه راه‌اندازی و اجرای برنامه‌ای که API گوگل ورک‌اسپیس را فراخوانی می‌کند، توضیح می‌دهند. این آموزش سریع از یک رویکرد احراز هویت ساده‌شده استفاده می‌کند که برای محیط آزمایشی مناسب است. برای محیط عملیاتی، توصیه می‌کنیم قبل از انتخاب اعتبارنامه‌های دسترسی مناسب برای برنامه خود، در مورد احراز هویت و مجوزدهی اطلاعات کسب کنید.

در Apps Script، پیش‌نمایش‌های Google Workspace از سرویس‌های پیشرفته گوگل برای فراخوانی APIهای Google Workspace و مدیریت برخی جزئیات جریان احراز هویت و مجوزدهی استفاده می‌کنند.

اهداف

  • محیط را پیکربندی کنید.
  • اسکریپت را ایجاد و پیکربندی کنید.
  • اسکریپت را اجرا کنید.

پیش‌نیازها

  • یک حساب گوگل
  • دسترسی به گوگل درایو

اسکریپت را ایجاد کنید

  1. با رفتن به script.google.com/create ، یک اسکریپت جدید در ویرایشگر Apps Script ایجاد کنید.
  2. محتویات ویرایشگر اسکریپت را با کد زیر جایگزین کنید:

درایو/فعالیت-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. روی پروژه بدون عنوان کلیک کنید، عبارت Quickstart را تایپ کنید و روی تغییر نام کلیک کنید.

پیکربندی اسکریپت

فعال کردن API فعالیت گوگل درایو

پروژه Apps Script را باز کنید.

  1. روی ویرایشگر کلیک کنید.
  2. در کنار سرویس‌ها ، روی سرویس کلیک کنید.
  3. API مربوط به فعالیت درایو (Drive Activity API) را انتخاب کنید و روی افزودن (Add) کلیک کنید.

نمونه را اجرا کنید

در ویرایشگر اسکریپت برنامه‌ها، روی اجرا (Run) کلیک کنید.

اولین باری که نمونه را اجرا می‌کنید، از شما اجازه دسترسی می‌خواهد:

  1. روی بررسی مجوزها کلیک کنید.
  2. یک حساب کاربری انتخاب کنید.
  3. روی اجازه دادن کلیک کنید.

گزارش اجرای اسکریپت در پایین پنجره ظاهر می‌شود.

مراحل بعدی