Google Apps Script 빠른 시작

Google Drive Activity API에 요청을 보내는 Google Apps Script 를 만듭니다.

빠른 시작에서는 Google Workspace API를 호출하는 앱을 설정하고 실행하는 방법을 설명합니다. 이 빠른 시작에서는 테스트 환경에 적합한 간소화된 인증 접근 방식을 사용합니다. 프로덕션 환경의 경우 앱에 적합한 액세스 사용자 인증 정보를 선택 하기 전에 인증 및 승인 에 대해 알아보는 것이 좋습니다.

Apps Script에서 Google Workspace 빠른 시작은 고급 Google 서비스를 사용하여 Google Workspace API를 호출하고 인증 및 승인 흐름의 일부 세부정보를 처리합니다.

목표

  • 환경 구성
  • 스크립트를 만들고 구성합니다.
  • 스크립트를 실행합니다.

기본 요건

  • Google 계정
  • Google Drive 액세스

스크립트 만들기

  1. script.google.com/create로 이동하여 Apps Script 스크립트 편집기에서 새 스크립트를 만듭니다.
  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 사용 설정

Apps Script 프로젝트를 엽니다.

  1. 편집기 를 클릭합니다.
  2. 서비스 옆에 있는 서비스 추가 를 클릭합니다 .
  3. Drive Activity API 를 선택하고 추가를 클릭합니다.

샘플 실행

Apps Script 편집기에서 실행 을 클릭합니다.

샘플을 처음 실행하면 액세스 권한을 부여하라는 메시지가 표시됩니다.

  1. 권한 검토 를 클릭합니다.
  2. 계정을 선택합니다.
  3. 허용 을 클릭합니다.

스크립트의 실행 로그가 창 하단에 표시됩니다.

다음 단계