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

从python命令行调用google api时出错,python,python-2.7,google-api-python-client,Python,Python 2.7,Google Api Python Client,我对python非常陌生。我正试图通过python命令行从google cloud shell调用google监控API。我正在使用python 2.7。在此之前,我已经安装了 pip install --upgrade google-api-python-client 这是调用google监控api的代码 from googleapiclient.discovery import build service = build('monitoring', 'v3', cache_dis

我对python非常陌生。我正试图通过python命令行从google cloud shell调用google监控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'
来自GoogleAppClient.discovery导入构建的
>>
>>>服务=构建('monitoring','v3',cache\u discovery=True)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/home/mugham/.local/lib/python2.7/site packages/googleapiclient/\u helpers.py”,第134行,位置包装中
已包装退货(*args,**kwargs)
文件“/home/mugham/.local/lib/python2.7/site packages/googleapiclient/discovery.py”,第282行,内部版本
adc_键路径=adc_键路径,
文件“/home/mugham/.local/lib/python2.7/site packages/googleapiclient/\u helpers.py”,第134行,位置包装中
已包装退货(*args,**kwargs)
文件“/home/mugham/.local/lib/python2.7/site packages/googleapiclient/discovery.py”,第489行,来自文档的内部版本
凭据=\u auth.default\u凭据()
文件“/home/mugham/.local/lib/python2.7/site packages/googleapiclient/_auth.py”,默认情况下第44行
凭据,uz=google.auth.default()
文件“/usr/local/lib/python2.7/dist packages/google/auth/_default.py”,默认为第338行
凭证,项目id=checker()
文件“/usr/local/lib/python2.7/dist-packages/google/auth/_-default.py”,第208行,在“get\U gae\U凭证”中
project\u id=app\u engine.get\u project\u id()
文件“/usr/local/lib/python2.7/dist packages/google/auth/app_engine.py”,第77行,在get_project_id中
返回应用程序标识。获取应用程序标识()
文件“/usr/lib/google cloud sdk/platform/google\u appengine/google/appengine/api/app\u identity/app\u identity.py”,第455行,在get\u应用程序id中
_,域名,显示应用程序id=\u ParseFullAppId(完整应用程序id)
文件“/usr/lib/google cloud sdk/platform/google\u appengine/google/appengine/api/app\u identity/app\u identity.py”,第436行,位于
psep=app\u id.find(\u分区\u分隔符)
AttributeError:“非类型”对象没有属性“查找”

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

我一直在用我的google cloud shell做这件事,所以我认为这可以在不传递google_应用程序_凭据的情况下工作,就像我为其他API(如compute engine)做的那样,但是看起来我需要从服务帐户创建一个密钥,该帐户有权通过此命令调用此监控api

gcloud iam服务帐户密钥创建--iam帐户输出密钥

完成后,我必须将该值导出到GOOGLE\u应用程序\u凭证中的环境文件中