I am very new to python. I am trying to call google monitoring API, through python command line from google cloud shell. I am using python 2.7. Before that I had already installed
pip install --upgrade google-api-python-client
This is the code for calling google monitoring 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()
I pass the appropriate values in python command line and when I execute this line
service = build('monitoring', 'v3', cache_discovery=True)
i get the following error:
>>>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'
I have no idea why this is coming, Can anyone please help me? Thanks
I had been doing this from my google cloud shell, so I thought that this would work without passing GOOGLE_APPLICATION_CREDENTIALS as I had done this for other apis like compute engine, but it looks like I need to create a key from the service account which has the privelige to call this monitoring api through this command,
gcloud iam service-accounts keys create --iam-account outputkey
Once this is done, then I had to export this value to the environment file in GOOGLE_APPLICATION_CREDENTIALS it worked.