更新或续订 Google Workspace

本页面介绍了如何使用 subscriptions.update() 方法续订 Google Workspace 订阅。您可以使用此方法更新订阅的到期时间,包括将订阅续订到最长的可能到期时间,或更新要接收的目标资源相关事件类型的列表。

Apps 脚本

  • Google Workspace 订阅。如需创建订阅,请参阅创建订阅

  • Apps 脚本项目:
    • 使用您的 Google Cloud 项目,而不是由 Apps 脚本自动创建的默认项目。
    • 对于您为配置 OAuth 权限请求页面而添加的所有范围,您还必须将这些范围添加到 Apps 脚本项目中的 appsscript.json 文件中。 例如,如果您指定了 chat.messages 范围,请添加以下内容:
    • "oauthScopes": [
        "https://www.googleapis.com/auth/chat.messages"
      ]
          
    • 启用Google Workspace Events 高级服务。

Python

  • Python 3.6 或更高版本
  • pip 软件包管理工具
  • 适用于 Python 的最新 Google 客户端库。如需安装或更新这些软件包,请在命令行界面中运行以下命令:
      pip3 install --upgrade google-api-python-client google-auth-oauthlib
      

续订 Google Workspace 订阅

在本部分中,您将使用 Google Workspace Events API 的 subscriptions.update() 方法将订阅续订到最长到期时间。如需指定最长到期时间,请将 Subscription 资源的 ttl 字段更新为 0

最长过期时间取决于事件载荷中包含的资源数据。如需详细了解过期时间,请参阅 Google Workspace 活动的活动数据

如需续订 Google Workspace 订阅,请执行以下操作:

Apps 脚本

  1. 在您的 Apps 脚本项目中,创建一个名为 updateSubscription 的新脚本文件,并添加以下代码:

    function updateSubscription() {
      // The name of the subscription to update.
      const name = 'subscriptions/SUBSCRIPTION_ID';
    
      // Call the Workspace Events API using the advanced service.
      const response = WorkspaceEvents.Subscriptions.patch({
        ttl: '0s',
      }, name);
      console.log(response);
    }
    

    替换以下内容:

    • SUBSCRIPTION_ID:订阅的 ID。如需获取 ID,您可以使用以下任一方法:
      • uid 字段的值。
      • 资源名称的 ID,以 name 字段表示。例如,如果资源名称为 subscriptions/subscription-123,请使用 subscription-123
  2. 如需更新 Google Workspace 订阅,请在您的 Apps 脚本项目中运行函数 updateSubscription

Python

  1. 在工作目录中,创建一个名为 update_subscription.py 的文件并添加以下代码:

    """Update subscription."""
    
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = [SCOPES]
    
    # Authenticate with Google Workspace and get user authentication.
    flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
    CREDENTIALS = flow.run_local_server()
    
    # Call the Workspace Events API using the service endpoint.
    service = build(
        'workspaceevents',
        'v1',
        credentials=CREDENTIALS,
    )
    
    BODY = {
        'ttl': {'seconds': 0},
    }
    NAME = 'subscriptions/SUBSCRIPTION_ID'
    response = (
        service.subscriptions()
        .patch(name=NAME, updateMask='ttl', body=BODY)
        .execute()
    )
    print(response)
    

    替换以下内容:

    • SCOPES:支持订阅的每种事件类型的一个或多个 OAuth 范围。格式为字符串数组。如需列出多个范围,请用英文逗号分隔。 例如 'https://www.googleapis.com/auth/chat.spaces.readonly', 'https://www.googleapis.com/auth/chat.memberships.readonly'
    • SUBSCRIPTION_ID:订阅的 ID。如需获取 ID,您可以使用以下任一方法:
      • uid 字段的值。
      • 资源名称的 ID,以 name 字段表示。例如,如果资源名称为 subscriptions/subscription-123,请使用 subscription-123
  2. 在工作目录中,确保您已存储 OAuth 客户端 ID 凭据并将文件命名为 credentials.json。此代码示例使用此 JSON 文件向 Google Workspace 进行身份验证并获取用户凭据。有关说明,请参阅创建 OAuth 客户端 ID 凭证

  3. 如需更新 Google Workspace 订阅,请在终端中运行以下命令:

    python3 update_subscription.py
Google Workspace Events API 会返回一个长时间运行的操作,其中包含 Subscription 资源的实例。

如需获取有关更新后的 Subscription 资源的详细信息,请使用 operations.get() 方法,并指定从 subscriptions.update() 请求返回的 Operation 资源。否则,如果您指定了订阅之前版本中的 Operation 资源,则响应为空。

以 Google Chat 扩展应用的身份更新或续订

您可以作为 Chat 应用(而非用户)更新或续订 Chat 事件。该流程类似,但有以下不同之处:

  1. 以 Chat 应用的身份进行身份验证,并获得管理员的一次性批准,而不是进行用户身份验证。

  2. 指定授权范围,以允许 Chat 应用订阅 Chat 事件。这些授权范围始终以 chat.app 开头,包括以下范围:

    • https://www.googleapis.com/auth/chat.app.memberships: 订阅 Chat 聊天室成员事件。
    • https://www.googleapis.com/auth/chat.app.messages.readonly: 订阅 Chat 聊天室消息事件。
    • https://www.googleapis.com/auth/chat.app.spaces:订阅 Chat 聊天室事件。

创建 API 密钥

如需调用开发者预览版 API 方法,您必须使用非公开的开发者预览版 API 发现文档。如需对请求进行身份验证,您必须传递 API 密钥。

如需创建 API 密钥,请打开应用的 Google Cloud 项目并执行以下操作:

  1. 在 Google Cloud 控制台中,依次前往菜单 > API 和服务 > 凭据

    进入“凭据”页面

  2. 依次点击创建凭据 > API 密钥
  3. 系统会显示您的新 API 密钥。
    • 点击“复制”图标 即可复制 API 密钥,以便在应用的代码中使用。您还可以在项目的凭据的“API 密钥”部分中找到 API 密钥。
    • 为防止未经授权的使用,我们建议您限制 API 密钥可用于哪些位置和 API。如需了解详情,请参阅添加 API 限制

编写调用 Google Workspace Events API 的脚本

以下代码示例用于更新 Chat 应用的 Google Workspace 订阅:

Python

  """Update subscription."""

  from google.oauth2 import service_account
  from apiclient.discovery import build

  # Specify required scopes.
  SCOPES = [SCOPES]

  # Specify service account details.
  CREDENTIALS = (
      service_account.Credentials.from_service_account_file('credentials.json')
      .with_scopes(SCOPES)
  )

  # Call the Workspace Events API using the service endpoint.
  service = build(
      'workspaceevents',
      'v1beta',
      credentials=CREDENTIALS,
      discoveryServiceUrl='https://workspaceevents.googleapis.com/$discovery/rest?version=v1beta&labels=DEVELOPER_PREVIEW&key=API_KEY',
  )

  BODY = {
      'ttl': {'seconds': 0},
  }
  NAME = 'subscriptions/SUBSCRIPTION_ID'
  response = (
      service.subscriptions()
      .patch(name=NAME, updateMask='ttl', body=BODY)
      .execute()
  )
  print(response)

替换以下内容:

  • SCOPES:指定授权范围,以允许 Chat 应用订阅 Chat 事件。这些授权范围始终以 chat.app 开头,包括以下范围:

    • https://www.googleapis.com/auth/chat.app.memberships: 订阅 Chat 聊天室成员事件。
    • https://www.googleapis.com/auth/chat.app.messages.readonly: 订阅聊天室消息和表情回应事件。
    • https://www.googleapis.com/auth/chat.app.spaces:订阅 Chat 聊天室事件。
  • API_KEY:您创建的用于构建 Google Workspace Events API 服务端点的 API 密钥。

  • SUBSCRIPTION_ID:订阅的 ID。如需获取 ID,您可以使用以下任一方法:

    • uid 字段的值。
    • 资源名称的 ID,以 name 字段表示。例如,如果资源名称为 subscriptions/subscription-123,请使用 subscription-123