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

调用Google Timezone API时timestamp参数无效报INVALID_REQUEST如何解决

Google Timezone API INVALID_REQUEST错误解决方案

核心问题1:timestamp参数单位错误

Google Timezone API要求传入的timestamp参数为秒级Unix时间戳,而JS中Date.now()、new Date().getTime()返回的都是13位的毫秒级时间戳,直接传入会不符合参数要求触发错误。

核心问题2:请求参数存在多余空格

你封装的请求URL中,location=${lat}, ${lng}的逗号后存在多余空格,不符合Google API的参数格式规范,也可能触发请求校验失败。

修正后的代码

封装请求action修正:

export const getTimezone = (lat, lng, timestamp) => (dispatch) => {
  axios
    .get(
      // 移除location参数的多余空格
      `https://maps.googleapis.com/maps/api/timezone/json?location=${lat},${lng}&timestamp=${timestamp}&key=API-KEY`
    )
    .then(({ data }) => {
      dispatch({ type: GET_TIMEZONE, payload: { timezone: data } });
    });
};

组件调用修正:

useEffect(() => {
  if (coordinates) {
    dispatch(
      // 毫秒级时间戳除以1000取整,转为10位秒级时间戳
      getTimezone(coordinates.lat, coordinates.lng, Math.floor(Date.now() / 1000))
    );
  }
}, [coordinates]);

额外校验说明

修改后如果仍有报错,可先打印传入的timestamp值,确认是10位整数,同时确认你的API_KEY已经开通Timezone API调用权限,且未超出调用配额。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.17 08:06:09