高级 Google Workspace 活动服务

利用高级 Google Workspace 事件服务,您可以在 Apps 脚本中使用 Google Workspace Events API。您可以通过此 API 订阅 Google Workspace 资源,接收您感兴趣的相关事件信息。事件代表资源发生了变化,例如创建、更新或删除了资源。

前提条件

  • 使用标准 Google Cloud 项目(而非由 Apps 脚本自动创建的默认项目)的 Apps 脚本项目。
  • 在同一 Google Cloud 项目中创建的 Pub/Sub 主题,用于接收订阅事件。如需创建 Pub/Sub 主题,请参阅创建和订阅 Pub/Sub 主题
  • 如需订阅 Chat 事件,您必须在 Google Cloud 控制台中的 Chat API 配置页面上配置 Google Chat 应用。如需创建 Google Chat 应用,请参阅使用 Apps 脚本构建 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 字段中的值。