高级 Google Workspace 活动服务

利用高级 Google Workspace 事件服务,您可以在 Apps 脚本中使用 Google Workspace Events API。借助此 API Google Workspace 资源,以便您收到符合自己需求的相关活动 。事件表示对资源的更改,例如当资源被 创建、更新或删除

前提条件

  • 使用标准 Google Cloud 项目(而非由 Apps 脚本自动创建的默认项目)的 Apps 脚本项目。
  • 在同一项目中创建的 Pub/Sub 主题 用于接收订阅事件的 Google Cloud 项目。 如需创建 Pub/Sub 主题,请参阅 创建并订阅 Pub/Sub 主题
  • 要订阅 Chat 事件,您必须拥有 在 Chat API 配置中配置的 Google Chat 应用 页面。如需创建 Google Chat 应用,请参阅使用 Apps 脚本构建 Google Chat 应用
  • 向 Apps 脚本添加的必要授权范围 项目的 appsscript.json 文件。必需的范围取决于 订阅的目标资源和事件。有关详情,请参阅 选择 Google Workspace Events API 范围。 例如:

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

参考

如需详细了解此服务,请参阅 Google Workspace Events API 参考文档。与 Apps 脚本中的所有高级服务一样, Google Workspace 事件服务使用相同的对象、方法和 作为公共 API 传递。

示例代码

这些示例展示了如何 Google Workspace Events API 使用高级服务执行操作

创建订阅

如需创建对 Google Workspace 资源的订阅,请添加 以下函数添加到 Apps 脚本项目代码中:

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 脚本项目的代码:

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 脚本项目的代码:

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 脚本项目的代码:

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 脚本项目的代码:

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 脚本项目的代码中:

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);
  }
}

如需获取操作的名称,请使用返回的 name 字段中的值 (如 subscriptions.create()subscriptions.patch()