如何使用API​​从GCP提取费用?

时间:2019-08-11 17:39:48

标签: api google-cloud-platform billing cost-management

我正在尝试使用api提取我们的GCP云的费用。第一步,我只是尝试提取给定区域中的实例列表。但是我只能得到以下信息,而没有实例信息:

XXX-MBP:gcp XXX $ gcloud beta auth应用程序-默认登录 您的浏览器已打开访问:

https://accounts.google.com/o/oauth2/auth?redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&prompt=select_account&response_type=code&client_id=XXX-XXX.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&access_type=offline

凭据已保存到文件:[/Users/XXX/.config/gcloud/application_default_credentials.json]

这些凭据将被任何请求使用的库使用 应用程序默认凭据。

要生成供其他用途的访问令牌,请运行:     gcloud auth应用程序-默认打印访问令牌 XXX-MBP:gcp XXX $

我的代码如下

from pprint import pprint

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

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

# Project ID for this request.
project = 'XXX'  # TODO: Update placeholder value.

# The name of the zone for this request.
zone = 'us-central1'  # TODO: Update placeholder value.

request = service.instances().list(project=project, zone=zone)
while request is not None:
    response = request.execute()

    for instance in response['items']:
        # TODO: Change code below to process each `instance` resource:
        pprint(instance)

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

我期望帐户中包含虚拟机列表。

有人可以帮忙吗?

0 个答案:

没有答案