Search code examples
google-analyticsgoogle-apigoogle-analytics-api

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.">
>>>

Solution

  • 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'.