googleapis / google-api-python-client

🐍 The official Python client library for Google's discovery based APIs.
https://googleapis.github.io/google-api-python-client/docs/
Apache License 2.0
7.66k stars 2.39k forks source link

feature request - support of catalog api #484

Closed MrMichaelWill closed 6 years ago

MrMichaelWill commented 6 years ago

https://cloud.google.com/billing/v1/how-tos/catalog-api is not currently in https://developers.google.com/api-client-library/python/apis/

I assume that means it is not implemented as a part of the google-api-python-client ?

How hard would it be to implement it?

Is there a good code example in any of the existing implementations to learn how to do so?

theacodes commented 6 years ago

The generated documentation isn't always up to date. This is showing up in the api explorer so it should work here:

cloudbilling = googleapiclient.discovery.build('cloudbilling', 'v1')
response = cloudbilling.services().list().execute()
MrMichaelWill commented 6 years ago

Not sure what you mean by not up to date. The cloud billing api is listed, I was talking about the catalog api instead, that would allow me to see prices for instances and services and such.

MrMichaelWill commented 6 years ago

Also your code snipped gets me

Traceback (most recent call last): File "test3b.py", line 5, in response = cloudbilling.services().list().execute() File "/usr/local/lib/python2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper return wrapped(*args, **kwargs) File "/usr/local/lib/python2.7/site-packages/googleapiclient/http.py", line 844, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 400 when requesting https://cloudbilling.googleapis.com/v1/services?alt=json returned "Request contains an invalid argument.">

This is with:

import googleapiclient.discovery cloudbilling = googleapiclient.discovery.build('cloudbilling', 'v1') response = cloudbilling.services().list().execute() print response

theacodes commented 6 years ago

The "Catalog API" is just a sub-resource in the cloud billing API.

Looks like that API just needs an API key, so just be sure to pass one with the developerKey argument to build.

MrMichaelWill commented 6 years ago

Thank you Jon, appreciated.

I am wondering, since I can do the following for the compute api, can I do something similar for the cloudbilling api as well?

compute = googleapiclient.discovery.build('compute', 'v1')

response = compute.zones().list(project='my-super-project').execute()

This works without me hardcoding or providing a key explicitely as a parameter because of having issued a gcloud auth login saved the necessary access credentials and/or tokens in ~/.config/gcloud/

Do I need to manage a separate key or is there a shorter path to use the same authentication for the googleapiclient cloudbilling resource ?

Cheers, Michael Will

On Tue, Mar 13, 2018 at 1:40 PM, Jon Wayne Parrott <notifications@github.com

wrote:

The "Catalog API" is just a sub-resource in the cloud billing API.

Looks like that API just needs an API key, so just be sure to pass one with the developerKey argument to build.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/google/google-api-python-client/issues/484#issuecomment-372810924, or mute the thread https://github.com/notifications/unsubscribe-auth/AAfdbtJZf9JbmO-mrIcptfJ3o68qdiSpks5teC7MgaJpZM4SpP7G .

theacodes commented 6 years ago

You can, it's just for some reason the cloud billing API doesn't expose its scopes, so you'll need to make sure the credentials have the cloud-platform scope:

credentials, _ = google.auth.default(scopes=['https://www.googleapis.com/auth/cloud-platform'])

.. = googleapiclient.discovery.build('cloudbilling', 'v1', credentials=credentials)
MrMichaelWill commented 6 years ago

Fails already earlier:

import google

import googleapiclient.discovery

credentials, _ = google.auth.default(scopes=[' https://www.googleapis.com/auth/cloud-platform'])

Traceback (most recent call last):

File "test4b.py", line 3, in

credentials, _ = google.auth.default(scopes=['

https://www.googleapis.com/auth/cloud-platform'])

AttributeError: 'module' object has no attribute 'auth'

On Tue, Mar 13, 2018 at 3:03 PM, Jon Wayne Parrott <notifications@github.com

wrote:

You can, it's just for some reason the cloud billing API doesn't expose its scopes, so you'll need to make sure the credentials have the cloud-platform scope:

credentials, _ = google.auth.default(scopes=['https://www.googleapis.com/auth/cloud-platform'])

.. = googleapiclient.discovery.build('cloudbilling', 'v1', credentials=credentials)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/google/google-api-python-client/issues/484#issuecomment-372834304, or mute the thread https://github.com/notifications/unsubscribe-auth/AAfdblkJGT1I2Sv1O4bHTelFcvsh_WaHks5teEI6gaJpZM4SpP7G .

theacodes commented 6 years ago

You'll need to install google-auth and google-auth-httplib2.

On Tue, Mar 13, 2018 at 3:22 PM MrMichaelWill notifications@github.com wrote:

Fails already earlier:

import google

import googleapiclient.discovery

credentials, _ = google.auth.default(scopes=[' https://www.googleapis.com/auth/cloud-platform'])

Traceback (most recent call last):

File "test4b.py", line 3, in

credentials, _ = google.auth.default(scopes=[' https://www.googleapis.com/auth/cloud-platform'])

AttributeError: 'module' object has no attribute 'auth'

On Tue, Mar 13, 2018 at 3:03 PM, Jon Wayne Parrott < notifications@github.com

wrote:

You can, it's just for some reason the cloud billing API doesn't expose its scopes, so you'll need to make sure the credentials have the cloud-platform scope:

credentials, _ = google.auth.default(scopes=[' https://www.googleapis.com/auth/cloud-platform'])

.. = googleapiclient.discovery.build('cloudbilling', 'v1', credentials=credentials)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub < https://github.com/google/google-api-python-client/issues/484#issuecomment-372834304 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AAfdblkJGT1I2Sv1O4bHTelFcvsh_WaHks5teEI6gaJpZM4SpP7G

.

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/google/google-api-python-client/issues/484#issuecomment-372839157, or mute the thread https://github.com/notifications/unsubscribe-auth/AAPUcwF4jAh7NngDjYYMNA546AUdXVaFks5teEa9gaJpZM4SpP7G .

MrMichaelWill commented 6 years ago

Done that, still not working:

import google import google.auth import googleapiclient.discovery

credentials, _ = google.auth.default(scopes=['https://www.googleapis.com/auth/cloud-platform']) cloudbilling = googleapiclient.discovery.build('cloudbilling', 'v1', credentials=credentials)

servicesList = cloudbilling.services().list().execute() print servicesList

Traceback (most recent call last): File "test3b.py", line 17, in servicesList = cloudbilling.services().list().execute() File "/usr/local/lib/python2.7/site-packages/oauth2client/util.py", line 137, in positional_wrapper return wrapped(*args, **kwargs) File "/usr/local/lib/python2.7/site-packages/googleapiclient/http.py", line 844, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 400 when requesting https://cloudbilling.googleapis.com/v1/services?alt=json returned "Request contains an invalid argument.">

I read that googleapiclient is in maintenance mode and I should just use google-cloud instead, but unfortunately that one does not support the catalog api either yet.

So maybe I have to use the better documented rest interface with my own curl calls instead...