Google 映射 timzezone API

Google maps timzezone API

在我目前正在开发的当前应用程序中,我们使用两种 google 地图 API:placestimezone。根据文档,我发现对于时区 API 我们不能使用 HTTP referrer 限制类型(但它适用于地点 API)。使用 timezone API 的 HTTP referrer,我收到错误 API keys with referer restrictions cannot be used with this API.,这对于时区 API 是有意义的,如果遵循文档。

但这里出现了问题...当我将应用程序服务器 IP 地址添加到 timezone API 的允许 IP 列表时,它仍然无法正常工作,我得到了一个像 This IP, site or mobile application is not authorized to use this API key. Request received from IP address xx.xxx.xxx.xx, with referer: {my app domain} 这样的错误,其中 xx.xxx.xxx.xx 是我的 IP 而不是服务器 IP。

可能是因为 javascript

调用了 timezone API
var url = "https://maps.googleapis.com/maps/api/timezone/json?key={API_KEY}?location={Ylocation},{Xlocation}&timestamp=" + d + "&sensor=false";
                                var ajaxObj = $.ajax({url:url, async:true, success:function(responseJson) {

这是否意味着我需要对服务器(后端)进行 ajax 调用,并且已经从那里进行了 timezone API 调用?如果是这样,也许还有另一种不重构当前逻辑的方法?

你说的是正确的,时区 API 只有在请求使用 API 密钥签名时才有效,该密钥根据 table here(虽然您也可以使用不受限制的 API 密钥,但这会使 API 密钥容易受到未经授权的使用。)

您提到使用具有 HTTP 引荐来源限制的 API 密钥适用于您的 Places API 实施。请注意,Places API 有四种类型:

  1. 地点 API(服务器端)
  2. Places 库(客户端)
  3. 适用于 Android 的 Places SDK(适用于 Android 个应用程序)
  4. 适用于 iOS 的 Places SDK(适用于 iOS 个应用程序)

如果您使用的是 Places Library,那么您可以使用 API 密钥签名并在客户端加载库时使用 HTTP 引荐来源限制。

另一方面,如果请求发生在使用 Places API 网络服务的服务器端,那么,就像在时区 API 中一样,您还需要使用 API 具有 IP 地址限制的密钥。

Web 服务调用,例如时区和地点 API 几乎可以与任何能够发送 URL 并接收 JSON 或 XML 作为响应的编程语言一起使用。但是,他们不会将 HTTP Referrer 数据发送到 API。

如果请求中使用的 API 键设置了 HTTP Referrer 限制,请求将被拒绝,因为缺少的 referrer 将不匹配列表中任何允许的 referrer。

根据这个 doc:

IP restrictions might be impractical, such as in mobile applications and cloud environments that rely on dynamic IP addresses. When using Maps Web Service APIs in these scenarios, secure your apps using one or more of the following techniques:

如果您仍然需要进一步的帮助,我建议您 file a support case 在 GCP 控制台上与我们一起打开一个个性化的沟通渠道,因为这个问题似乎与编程无关。

希望对您有所帮助!