更新或续订 Google Workspace

本页面介绍如何使用 subscriptions.update() 方法续订 Google Workspace。您可以使用此方法来更新订阅的到期时间,包括尽可能续订订阅。

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,请执行以下操作:

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('client_secrets.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 凭据并将文件命名为 client_secrets.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 资源,则响应为空。