更新或续订 Google Workspace

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

Apps 脚本

  • 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 资源,则响应为空。