api - 谷歌云平台 : List available projects using api

标签 api google-cloud-platform gcloud google-cloud-iam

我想达到和gcloud projects list一样的效果特别是通过 api 调用( python )。

但是,我在浏览文档时遇到的唯一问题就是这个。

此操作是否专门与资源管理器 API 相关联?

对其他项目的可见性会发生什么?如果 RM 绑定(bind)到一个项目,它如何查看(并因此列出)其他项目?

最佳答案

您只能列出您有权访问的项目。这意味着您无法看到所有项目,除非您有权访问它们。在下面的示例中,我展示了需要哪些范围。这也意味着您可以跨帐户列出项目。这允许您使用示例中指定的凭据查看您有权访问哪些项目。我展示了如何使用应用程序默认凭据 (ADC) 和服务帐户凭据(Json 文件格式)。

更多信息可以阅读我的文章here关于项目。

这些示例已在 Windows 10 Professional 上使用 Python 3.6 进行了测试。这些示例将完全按照 CLI 显示项目列表。

使用 Python 客户端库的示例 1(服务发现方法):

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from google.oauth2 import service_account

# Example using the Python Client Library

# Documentation
# https://github.com/googleapis/google-api-python-client
# https://developers.google.com/resources/api-libraries/documentation/cloudresourcemanager/v2/python/latest/

# Library Installation
# pip install -U google-api-python-client
# pip install -U oauth2client

# Requires one of the following scopes
# https://www.googleapis.com/auth/cloud-platform
# https://www.googleapis.com/auth/cloud-platform.read-only
# https://www.googleapis.com/auth/cloudplatformprojects
# https://www.googleapis.com/auth/cloudplatformprojects.readonly

print('{:<20} {:<22} {:<21}'.format('PROJECT_ID', 'NAME', 'PROJECT_NUMBER'))

# Uncomment to use Application Default Credentials (ADC)
credentials = GoogleCredentials.get_application_default()

# Uncomment to use Service Account Credentials in Json format
# credentials = service_account.Credentials.from_service_account_file('service-account.json')

service = discovery.build('cloudresourcemanager', 'v1', credentials=credentials)

request = service.projects().list()

while request is not None:
    response = request.execute()

    for project in response.get('projects', []):
        print('{:<20} {:<22} {:<21}'.format(project['projectId'], project['name'], project['projectNumber']))

    request = service.projects().list_next(previous_request=request, previous_response=response)

示例 2 使用 Python Google Cloud Resource Manager API 客户端库:
from google.cloud import resource_manager

# Example using the Python Google Cloud Resource Manager API Client Library

# Documentation
# https://pypi.org/project/google-cloud-resource-manager/
# https://github.com/googleapis/google-cloud-python
# https://googleapis.github.io/google-cloud-python/latest/resource-manager/index.html
# https://googleapis.github.io/google-cloud-python/latest/resource-manager/client.html
# https://googleapis.github.io/google-cloud-python/latest/resource-manager/project.html

# Library Installation
# pip install -U google-cloud-resource-manager

# Requires one of the following scopes
# https://www.googleapis.com/auth/cloud-platform
# https://www.googleapis.com/auth/cloud-platform.read-only
# https://www.googleapis.com/auth/cloudplatformprojects
# https://www.googleapis.com/auth/cloudplatformprojects.readonly

print('{:<20} {:<22} {:<21}'.format('PROJECT_ID', 'NAME', 'PROJECT_NUMBER'))

# Uncomment to use Application Default Credentials (ADC)
client = resource_manager.Client()

# Uncomment to use Service Account Credentials in Json format
# client = resource_manager.Client.from_service_account_json('service-account.json')

for project in client.list_projects():
    print('{:<20} {:<22} {:<21}'.format(project.project_id, project.name, project.number))

关于api - 谷歌云平台 : List available projects using api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58366396/

相关文章:

使用容器操作系统的 GCP 中本地 SSD 的 NVMe 与 SCSI 的性能

swift - 401 alamofire 获取带有 header 的请求

api - Spring Security 中接收 token 的基本身份验证

php - 检查YouTube用户是否存在PHP

python - 如何 : Write Python API wrapper?

google-cloud-platform - 为特定项目和集群运行单个 kubectl 命令?

python-3.x - GCP-The App Engine APIs are not available, with py 3

windows-10 - 如何将 gcloud 添加到 Windows 10 中的路径

node.js - 无法选择区域和/或区域。部署 NodeJS 托管虚拟机应用程序时

google-cloud-platform - 将镜像从 Docker 注册表移动到 GCR