Son*_*han 9 python-3.x google-api-client google-cloud-platform
我想使用 python google client api 获取项目中所有实例的列表google-api-python-client==1.7.11
我正在尝试使用googleapiclient.discovery.build此方法需要凭据作为参数的方法进行连接
我阅读了文档,但没有获得凭证格式以及它需要的凭证
任何人都可以解释什么凭据以及如何传递以建立 gcp 连接
Joh*_*ley 14
您需要的凭据称为“服务帐户 JSON 密钥文件”。这些是在 Google Cloud Console 中的 IAM & Admin / Service Accounts 下创建的。创建服务帐户并下载密钥文件。在下面的示例中,这是service-account.json.
使用服务帐号的示例代码:
from googleapiclient import discovery
from google.oauth2 import service_account
scopes = ['https://www.googleapis.com/auth/cloud-platform']
sa_file = 'service-account.json'
zone = 'us-central1-a'
project_id = 'my_project_id' # Project ID, not Project Name
credentials = service_account.Credentials.from_service_account_file(sa_file, scopes=scopes)
# Create the Cloud Compute Engine service object
service = discovery.build('compute', 'v1', credentials=credentials)
request = service.instances().list(project=project_id, zone=zone)
while request is not None:
response = request.execute()
for instance in response['items']:
# TODO: Change code below to process each `instance` resource:
print(instance)
request = service.instances().list_next(previous_request=request, previous_response=response)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2802 次 |
| 最近记录: |