Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Discovery API The request is missing a valid API key

We've been using the google discovery api for analytics setup to make the request: "https://analytics.googleapis.com/$discovery/rest?version=v4" This has been working fine for over two years now, however just today it's started responding with:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://analytics.googleapis.com/$discovery/rest?version=v4 returned "The request is missing a valid API key.">

I cannot seem to find any status on this api but I feel as if something might have changed.

Manual testing has been performed and I've replicated the issue consistently.

>>> credentials = oauth2client.client.GoogleCredentials("<redacted>", "<redacted>", "<redacted>", "<redacted>", None, "https://accounts.google.com/o/oauth2/token", "UserAgentHere")

>>> credentials
<oauth2client.client.GoogleCredentials object at 0x7f533eaf60b8>

>>> import httplib2

>>> http = credentials.authorize(http=httplib2.Http())

>>> http
<httplib2.Http object at 0x7f533eaf6390>

>>> from apiclient.discovery import build

>>> build("analytics", "v4", http=http, cache_discovery=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/googleapiclient/discovery.py", line 232, in build
    raise e
  File "/usr/lib/python3.6/site-packages/googleapiclient/discovery.py", line 224, in build
    requested_url, discovery_http, cache_discovery, cache, developerKey)
  File "/usr/lib/python3.6/site-packages/googleapiclient/discovery.py", line 277, in _retrieve_discovery_doc
    raise HttpError(resp, content, uri=actual_url)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://analytics.googleapis.com/$discovery/rest?version=v4 returned "The request is missing a valid API key.">
>>>
like image 503
Karl Kloppenborg Avatar asked Jul 11 '19 01:07

Karl Kloppenborg


2 Answers

Try to change discoveryServiceUrl to

'https://analyticsreporting.googleapis.com/$discovery/rest?version=v4'

Example:

build("analytics", "v4", http=http, cache_discovery=False, 
    discoveryServiceUrl='https://analyticsreporting.googleapis.com/$discovery/rest?version=v4'
)

but it only works for reporting, not for management api

Update:

You can also change service name from 'analytics' to 'analyticsreporting'.

build("analyticsreporting", "v4", http=http, cache_discovery=False,)

'analytics' service name was used for 'api version 3' and was the same for all scopes. If you're using a 'reporting core api v4', you should change the name to 'analyticsreporting'.

like image 178
Andrey Zavodov Avatar answered Oct 21 '22 22:10

Andrey Zavodov


Whilst the answer from Andrey Avodov works, I believe it is more correct to replace 'analytics' with 'analyticsreporting'

i.e. replace

build("analytics", "v4", http=http, cache_discovery=False)

with

build("analyticsreporting", "v4", http=http, cache_discovery=False)

The 'analytics' version had been working for me for years, but stopped working this morning. Replacing it with the new code has fixed that.

This is as per the v4 quickstart guide https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py

It also ties up with the discovery code which already has a discovery url in the correct format (so long as you pass analyticsreporting

V2_DISCOVERY_URI = ('https://{api}.googleapis.com/$discovery/rest?'
                    'version={apiVersion}')

...
snip
...


for discovery_url in (discoveryServiceUrl, V2_DISCOVERY_URI,):
    requested_url = uritemplate.expand(discovery_url, params)

from https://github.com/googleapis/google-api-python-client/blob/b854ff13c801b98f97ff3b9a2ddbd9af54724b9a/googleapiclient/discovery.py

I assume the analytics version has been deprecated / broken somehow, but given the quickstart uses the analyticsreporting version this feels like a safer / more complete change than updating the discovery url.

like image 34
Duncan Morris Avatar answered Oct 21 '22 23:10

Duncan Morris