You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Google Crux API用collectionPeriod设日期范围遇字段不存在错误

解决Google Crux API的collectionPeriod参数错误问题

你遇到的错误是因为Google Crux API的queryRecord接口并不支持collectionPeriod这个字段,正确的日期范围参数名为dateRange,且子字段需使用startDate和endDate,而非firstDate和lastDate。

修正后的代码

import requests

url = "https://chromeuxreport.googleapis.com/v1/records:queryRecord?key=你的API密钥"

headers = {"Content-Type": "application/json",
          "Accept": "application/json"}

data = {
    "origin": "https://www.example1.com",
    "metrics": ["largest_contentful_paint", "experimental_time_to_first_byte"],
    "dateRange": {
        "startDate": {
            "year": 2022,
            "month": 10,
            "day": 12
        },
        "endDate": {
            "year": 2022,
            "month": 11,
            "day": 1
        }
    }
}

response = requests.post(url, json=data, headers=headers)

print(response.json())

关键修改点

  • 将原请求体中的collectionPeriod替换为API官方定义的dateRange字段
  • 把firstDate改为startDate,lastDate改为endDate,匹配API的参数命名规范

注意事项

  • 确保日期范围符合API限制:Crux API仅提供最近28天的滚动用户体验数据,超出范围的日期可能返回无数据或错误
  • 月份取值为1-12,日期需为对应月份的有效天数
  • 替换代码中的你的API密钥为实际的Google Cloud API密钥

内容的提问来源于stack exchange,提问作者KristiLuna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 20:20:35