Python 快速入门

创建一个 Python 命令行应用,用于向 Google 保险柜 API 发出请求。

快速入门介绍了如何设置和运行调用 Google Workspace API 的应用。本快速入门使用一种简化的身份验证方法,适用于测试环境。对于生产环境,我们建议您先了解 身份验证和授权 ,然后再 选择适合您应用的访问凭据 。

本快速入门使用 Google Workspace 推荐的 API 客户端库来处理身份验证和授权流程的一些详细信息。

目标

  • 设置环境。
  • 安装客户端库。
  • 设置示例。
  • 运行示例。

前提条件

如需运行本快速入门,您需要满足以下前提条件:

  • Google 账号。

设置环境

如需完成本快速入门,请设置您的环境。

启用 API

在使用 Google API 之前,您需要在 Google Cloud 项目中启用它们。 您可以在单个 Google Cloud 项目中启用一个或多个 API。
  • 在 Google Cloud 控制台中,启用 Google 保险柜 API。

    启用 API

如果您使用新的 Google Cloud 云项目来完成本快速入门,请配置 OAuth 权限请求页面。如果您已为云项目完成此步骤,请跳到下一部分。

  1. 在 Google API 控制台中,依次点击菜单 > Google Auth 平台 > 品牌塑造。

    前往“品牌塑造”

  2. 如果您已配置 Google Auth 平台,则可以在品牌塑造、受众群体和数据访问权限中配置以下 OAuth 权限请求页面设置。如果您看到一条消息,提示尚未配置 Google Auth 平台,请点击开始使用:
    1. 在应用信息 下的应用名称 中,输入应用的名称。
    2. 在用户支持邮箱中,选择一个支持邮箱地址,以便用户在对权限请求有疑问时与您联系。
    3. 点击下一步 。
    4. 在受众群体 下,选择内部 。
    5. 点击下一步 。
    6. 在联系信息下,输入一个邮箱地址,以便您接收有关项目变更的通知。
    7. 点击下一步 。
    8. 在完成 部分,查看《Google API 服务用户数据政策》,如果您同意该政策,请选择我同意 Google API 服务:用户数据政策。
    9. 点击继续 。
    10. 点击创建 。
  3. 目前,您可以跳过添加范围。 将来,当您创建的应用要在 Google Workspace 组织外部使用时,您必须将 用户类型 更改为 外部 。然后, 添加应用所需的授权范围。如需了解详情,请参阅完整的 配置 OAuth 权限请求页面指南。

为桌面应用授权凭据

如需对最终用户进行身份验证并访问应用中的用户数据,您需要创建一个或多个 OAuth 2.0 客户端 ID。客户端 ID 用于向 Google 的 OAuth 服务器标识单个应用。如果您的应用在多个平台上运行,您必须为每个平台创建一个单独的客户端 ID。
  1. 在 Google Cloud 控制台中,依次点击菜单 > Google Auth 平台 > 客户端。

    前往“客户端”

  2. 点击创建客户端 。
  3. 依次点击应用类型 > 桌面应用。
  4. 在名称 字段中,输入凭据的名称。此名称仅在 Google Cloud 控制台中显示。
  5. 点击创建 。

    新创建的凭据会显示在“OAuth 2.0 客户端 ID”下。

  6. 将下载的 JSON 文件另存为 credentials.json,然后将该文件移到您的工作目录。

安装 Google 客户端库

  • 安装 Python 版 Google 客户端库:

    python3 -m pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
    

配置示例

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

    vault/quickstart/quickstart.py
    import os.path
    
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError
    
    # If modifying these scopes, delete the file token.json.
    SCOPES = ["https://www.googleapis.com/auth/ediscovery"]
    
    
    def main():
      """Shows basic usage of the Vault API.
      Prints the names and IDs of the first 10 matters in Vault.
      """
      creds = None
      # The file token.json stores the user's access and refresh tokens, and is
      # created automatically when the authorization flow completes for the first
      # time.
      if os.path.exists("token.json"):
        creds = Credentials.from_authorized_user_file("token.json", SCOPES)
      # If there are no (valid) credentials available, let the user log in.
      if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
          creds.refresh(Request())
        else:
          flow = InstalledAppFlow.from_client_secrets_file(
              "credentials.json", SCOPES
          )
          creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open("token.json", "w") as token:
          token.write(creds.to_json())
    
      try:
        service = build("vault", "v1", credentials=creds)
    
        # Call the Vault API
        results = service.matters().list(pageSize=10).execute()
        matters = results.get("matters", [])
    
        if not matters:
          print("No matters found.")
          return
    
        print("Matters:")
        for matter in matters:
          print(f"{matter.get('name')} ({matter.get('id')})")
      except HttpError as err:
        print(err)
    
    
    if __name__ == "__main__":
      main()

运行示例

  1. 在工作目录中,构建并运行示例:

    python3 quickstart.py
    
  1. 首次运行示例时,系统会提示您授权访问:
    1. 如果您尚未登录 Google 账号,请在系统提示时登录。如果您登录了多个账号,请选择一个账号用于授权。
    2. 点击接受 。

    您的 Python 应用会运行并调用 Google 保险柜 API。

    授权信息存储在文件系统中,因此下次运行示例 代码时,系统不会提示您进行授权。

后续步骤