繁体   English   中英

从 python 命令行调用 google api 时出错

[英]error while calling google api from python command line

我对python很陌生。 我正在尝试通过来自google cloud shell 的python 命令行调用 google monitoring API。 我正在使用 python 2.7。 在此之前我已经安装了

pip install --upgrade google-api-python-client    

这是调用google监控api的代码

from googleapiclient.discovery import build

service = build('monitoring', 'v3', cache_discovery=True)
timeseries = service.projects().timeSeries().list(
            name="my-project",
            filter="metric.type=compute.googleapis.com/instance/cpu/utilization"
            aggregation_alignmentPeriod=86400s,
            # aggregation_crossSeriesReducer=api_args["crossSeriesReducer"],
            aggregation_perSeriesAligner="ALIGN_MEAN",
            aggregation_groupByFields="metric.labels.key",
            interval_endTime="2020-08-23T17:30:04.707Z",
            interval_startTime="2020-08-22T17:30:05.603000Z",
            pageSize=100
        ).execute()

我在 python 命令行和执行此行时传递了适当的值

service = build('monitoring', 'v3', cache_discovery=True)

我收到以下错误:

>>>from googleapiclient.discovery import build
>>> service = build('monitoring', 'v3', cache_discovery=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mugham/.local/lib/python2.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/mugham/.local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 282, in build
    adc_key_path=adc_key_path,
  File "/home/mugham/.local/lib/python2.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/mugham/.local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 489, in build_from_document
    credentials = _auth.default_credentials()
  File "/home/mugham/.local/lib/python2.7/site-packages/googleapiclient/_auth.py", line 44, in default_credentials
    credentials, _ = google.auth.default()
  File "/usr/local/lib/python2.7/dist-packages/google/auth/_default.py", line 338, in default
    credentials, project_id = checker()
  File "/usr/local/lib/python2.7/dist-packages/google/auth/_default.py", line 208, in _get_gae_credentials
    project_id = app_engine.get_project_id()
  File "/usr/local/lib/python2.7/dist-packages/google/auth/app_engine.py", line 77, in get_project_id
    return app_identity.get_application_id()
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/api/app_identity/app_identity.py", line 455, in get_application_id
    _, domain_name, display_app_id = _ParseFullAppId(full_app_id)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/api/app_identity/app_identity.py", line 436, in _ParseFullAppId
    psep = app_id.find(_PARTITION_SEPARATOR)
AttributeError: 'NoneType' object has no attribute 'find'

我不知道为什么会这样,有人可以帮助我吗? 谢谢

我已经从我的谷歌云壳这样做,所以我想,这将不经过GOOGLE_APPLICATION_CREDENTIALS因为我已经做到了这一点对于其他API像计算引擎的工作,但它看起来像我需要建立从具有服务帐户的关键通过这个命令调用这个监控api的特权,

gcloud iam service-accounts 密钥创建 --iam-account outputkey

完成此操作后,我必须将此值导出到GOOGLE_APPLICATION_CREDENTIALS 中的环境文件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM