【问题标题】:Google Discovery API The request is missing a valid API keyGoogle Discovery API 请求缺少有效的 API 密钥
【发布时间】:2019-07-11 01:38:50
【问题描述】:

我们一直在使用 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.">
>>>

【问题讨论】:

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


    【解决方案1】:

    尝试将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

    更新

    您还可以将服务名称从“分析”更改为“分析报告”。

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

    'analytics' 服务名称用于 'api 版本 3',并且对于所有范围都相同。如果您使用的是'reporting core api v4',则应将名称更改为'analyticsreporting'

    【讨论】:

    • 对于任何怀疑这个答案的人来说,Updated 版本对我来说非常有用,我已经用analytics 运行 v4 代码几个月了,今天它刚刚坏了/改变了.
    【解决方案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 更安全/更完整。

    【讨论】:

      猜你喜欢
      • 2018-11-07
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多