更新或续订 Google Workspace

本页面介绍了如何使用 the 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() 方法将订阅续订到最长到期时间。如需指定最长到期时间,请将 ttl 资源的 Subscription 字段 更新为 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 字段的值。
      • name 字段中表示的资源名称的 ID。例如,如果资源名称为 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 字段的值。
      • name 字段中表示的资源名称的 ID。例如,如果资源名称为 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() 方法 并指定从 Operation 请求返回的 subscriptions.update() 资源。否则,如果您指定了订阅的先前版本的 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.memberships.readonly:订阅 Chat 聊天室成员事件。
    • https://www.googleapis.com/auth/chat.app.messages.readonly:订阅 Chat 聊天室消息事件。
    • https://www.googleapis.com/auth/chat.app.spaces:订阅 Chat 聊天室事件。
    • https://www.googleapis.com/auth/chat.app.spaces.readonly:订阅 Chat 聊天室事件。

编写调用 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,
  )

  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.memberships.readonly:订阅 Chat 聊天室成员事件。
    • https://www.googleapis.com/auth/chat.app.messages.readonly:订阅 Chat 聊天室消息和反应事件。
    • https://www.googleapis.com/auth/chat.app.spaces:订阅 Chat 聊天室事件。
    • https://www.googleapis.com/auth/chat.app.spaces.readonly:订阅 Chat 聊天室事件。
  • SUBSCRIPTION_ID:订阅的 ID。如需获取 ID,您可以使用以下任一方法:

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