You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python脚本获取当前所有GCP服务及相关API列表?

用Python获取最新GCP服务及API列表

当然可以实现,核心是调用GCP的Discovery API——这个API专门提供GCP所有服务的实时元数据,每次调用都能拿到当前最新的服务信息。

1. 安装依赖

先安装Google官方的Python客户端库:

pip install google-api-python-client

2. 编写Python脚本

以下脚本会遍历所有GCP服务,提取并打印服务名称和对应API的标题:

from googleapiclient import discovery
from googleapiclient.errors import HttpError

def get_all_gcp_services():
    # 构建Discovery API客户端
    discovery_client = discovery.build('discovery', 'v1')
    
    all_services = []
    page_token = None
    
    try:
        while True:
            # 请求服务列表,支持分页处理
            response = discovery_client.services().list(pageToken=page_token).execute()
            all_services.extend(response.get('items', []))
            
            # 检查是否有下一页数据
            page_token = response.get('nextPageToken')
            if not page_token:
                break
        
        # 输出整理后的服务信息
        print("GCP服务及对应API列表:")
        print("-" * 80)
        for service in all_services:
            service_name = service.get('name')
            api_title = service.get('title')
            print(f"服务名称: {service_name}")
            print(f"API标题: {api_title}")
            print("-" * 80)
            
        return all_services
    
    except HttpError as err:
        print(f"请求出错: {err}")
        return None

if __name__ == "__main__":
    get_all_gcp_services()

关键说明

  • 实时获取最新数据:Discovery API返回的是GCP当前公开可用的所有服务元数据,每次运行脚本都是实时调用,确保信息最新。
  • 分页处理:当GCP服务数量较多时,API会返回分页结果,脚本通过nextPageToken循环获取所有页面内容,不会遗漏任何服务。
  • 无需额外权限:调用Discovery API不需要身份验证(公开可访问),脚本可以直接运行,无需配置GCP密钥或服务账号。

内容的提问来源于stack exchange,提问作者Danny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 05:07:31