Google Discovery API该请求缺少有效的API密钥

时间:2019-07-11 01:38:50

标签: google-analytics google-api google-analytics-api

我们一直在使用Google Discovery API进行分析设置以发出请求:“ https://analytics.googleapis.com/ $ discovery / rest?version = v4” 到目前为止,这种方法已经运行了两年多,但是直到今天它才开始回应:

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

我似乎无法在此api上找到任何状态,但感觉好像有些变化。

已经进行了手动测试,我已经一贯地复制了该问题。

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

2 个答案:

答案 0 :(得分:7)

尝试将DiscoveryServiceUrl更改为

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

示例:

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

但仅适用于报告,不适用于管理API

更新

您还可以将服务名称从“ analytics”更改为“ analyticsreporting”。

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

'analytics'服务名称用于'api版本3',并且在所有范围内都相同。如果您使用的是“报告核心api v4” ,则应将名称更改为“ analyticsreporting”

答案 1 :(得分:2)

尽管安德烈·阿沃多夫(Andrey Avodov)的答案行得通,但我认为用“分析报告”代替“分析”更为正确

即替换

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

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

“分析”版本已经为我服务了好多年,但是今天早上停止了工作。用新代码替换它可以解决此问题。

这是根据v4快速入门指南https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py

它还与发现代码绑定在一起,发现代码已经具有正确格式的发现url(只要您传递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)

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

我认为分析版本已经过时/损坏,但是考虑到快速入门使用的是analyticsreporting版本,这比更新发现url感觉更安全/更完整。