고급 Google Workspace 이벤트 서비스

고급 Google Workspace 이벤트 서비스를 사용하면 Apps Script에서 Google Workspace Events API를 사용할 수 있습니다. 이 API를 사용하면 Google Workspace 리소스를 구독하여 관심 있는 관련 이벤트를 수신할 수 있습니다. 이벤트는 리소스의 생성, 업데이트, 삭제와 같은 리소스의 변경사항을 나타냅니다.

기본 요건

  • Apps Script에서 자동으로 생성된 기본 프로젝트 대신 표준 Google Cloud 프로젝트를 사용하는 Apps Script 프로젝트
  • 구독 이벤트를 수신하기 위해 동일한 Google Cloud 프로젝트에서 생성된 Pub/Sub 주제 Pub/Sub 주제를 만들려면 Pub/Sub 주제 만들기 및 구독을 참조하세요.
  • Chat 이벤트를 구독하려면 Google Cloud 콘솔의 Chat API 구성 페이지에 Google Chat 앱을 구성해야 합니다. Google Chat 앱을 만들려면 Apps Script로 Google Chat 앱 빌드를 참고하세요.
  • Apps Script 프로젝트의 appsscript.json 파일에 추가된 필수 승인 범위 필요한 범위는 정기 결제의 대상 리소스 및 이벤트 유형에 따라 다릅니다. 자세한 내용은 Google Workspace Events API 범위 선택하기를 참고하세요. 예를 들면 다음과 같습니다.

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.messages.readonly"
    ]
    

참조

이 서비스에 대한 자세한 내용은 Google Workspace Events API 참조 문서를 확인하세요. Apps Script의 모든 고급 서비스와 마찬가지로 Google Workspace 이벤트 서비스는 공개 API와 동일한 객체, 메서드, 매개변수를 사용합니다.

샘플 코드

이 샘플은 고급 서비스를 사용하여 일반적인 Google Workspace Events API 작업을 수행하는 방법을 보여줍니다.

구독 만들기

Google Workspace 리소스에 대한 구독을 만들려면 Apps Script 프로젝트의 코드에 다음 함수를 추가합니다.

advanced/events.gs
/**
 * Creates a subscription to receive events about a Google Workspace resource.
 * For a list of supported resources and event types, see the
 * [Google Workspace Events API Overview](https://developers.google.com/workspace/events#supported-events).
 * For additional information, see the
 * [subscriptions.create](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/create)
 * method reference.
 * @param {!string} targetResource The full resource name of the Google Workspace resource to subscribe to.
 * @param {!string|!Array<string>} eventTypes The types of events to receive about the resource.
 * @param {!string} pubsubTopic The resource name of the Pub/Sub topic that receives events from the subscription.
 */
function createSubscription(targetResource, eventTypes, pubsubTopic) {
  try {
    const operation = WorkspaceEvents.Subscriptions.create({
      targetResource: targetResource,
      eventTypes: eventTypes,
      notificationEndpoint: {
        pubsubTopic: pubsubTopic,
      },
    });
    console.log(operation);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to create subscription with error %s', err.message);
  }
}

구독 나열

이벤트 유형 및 대상 리소스별로 필터링된 구독을 나열하려면 Apps Script 프로젝트의 코드에 다음 함수를 추가합니다.

advanced/events.gs
/**
 * Lists subscriptions created by the calling app filtered by one or more event types and optionally by a target resource.
 * For additional information, see the
 * [subscriptions.list](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/list)
 * method reference.
 * @param {!string} filter The query filter.
 */
function listSubscriptions(filter) {
  try {
    const response = WorkspaceEvents.Subscriptions.list({ filter });
    console.log(response);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to list subscriptions with error %s', err.message);
  }
}

구독 가져오기

구독에 관한 정보를 가져오려면 Apps Script 프로젝트의 코드에 다음 함수를 추가합니다.

advanced/events.gs
/**
 * Gets details about a subscription.
 * For additional information, see the
 * [subscriptions.get](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/get)
 * method reference.
 * @param {!string} name The resource name of the subscription.
 */
function getSubscription(name) {
  try {
    const subscription = WorkspaceEvents.Subscriptions.get(name);
    console.log(subscription);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to get subscription with error %s', err.message);
  }
}

구독 업데이트

정기 결제를 업데이트하거나 갱신하려면 Apps Script 프로젝트의 코드에 다음 함수를 추가합니다.

advanced/events.gs
/**
 * Updates an existing subscription.
 * This can be used to renew a subscription that is about to expire.
 * For additional information, see the
 * [subscriptions.patch](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/patch)
 * method reference.
 * @param {!string} name The resource name of the subscription.
 */
function patchSubscription(name) {
  try {
    const operation = WorkspaceEvents.Subscriptions.patch({
      // Setting the TTL to 0 seconds extends the subscription to its maximum expiration time.
      ttl: '0s',
    }, name);
    console.log(operation);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to update subscription with error %s', err.message);
  }
}

구독 재활성화

구독을 다시 활성화하려면 다음 함수를 Apps Script 프로젝트의 코드에 추가합니다.

advanced/events.gs
/**
 * Reactivates a suspended subscription.
 * Before reactivating, you must resolve any errors with the subscription.
 * For additional information, see the
 * [subscriptions.reactivate](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/reactivate)
 * method reference.
 * @param {!string} name The resource name of the subscription.
 */
function reactivateSubscription(name) {
  try {
    const operation = WorkspaceEvents.Subscriptions.reactivate({}, name);
    console.log(operation);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to reactivate subscription with error %s', err.message);
  }
}

구독 삭제

구독을 삭제하려면 다음 함수를 Apps Script 프로젝트의 코드에 추가합니다.

advanced/events.gs
/**
 * Deletes a subscription.
 * For additional information, see the
 * [subscriptions.delete](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/delete)
 * method reference.
 * @param {!string} name The resource name of the subscription.
 */
function deleteSubscription(name) {
  try {
    const operation = WorkspaceEvents.Subscriptions.remove(name);
    console.log(operation);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to delete subscription with error %s', err.message);
  }
}

작업 가져오기

대부분의 Google Workspace Events API 메서드는 장기 실행 작업을 반환합니다. 작업 상태를 확인하려면 operations.get() 메서드를 사용하면 됩니다.

작업에 대한 정보를 가져오려면 Apps Script 프로젝트의 코드에 다음 함수를 추가합니다.

advanced/events.gs
/**
 * Gets details about an operation returned by one of the methods on the subscription
 * resource of the Google Workspace Events API.
 * For additional information, see the
 * [operations.get](https://developers.google.com/workspace/events/reference/rest/v1/operations/get)
 * method reference.
 * @param {!string} name The resource name of the operation.
 */
function getOperation(name) {
  try {
    const operation = WorkspaceEvents.Operations.get(name);
    console.log(operation);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to get operation with error %s', err.message);
  }
}

작업 이름을 가져오려면 Google Workspace Events API 메서드(예: subscriptions.create() 또는 subscriptions.patch()) 중 하나에서 반환된 name 필드의 값을 사용합니다.