Google 地图 API 关键 HTTP 限制导致地理编码出现 403 错误 API

Google maps API key HTTP restriction causes 403 error in geocoding API

在将密钥限制到我的域后,我在 google 地图 API 地理编码 API 仪表板中收到 403 错误。地理编码在密钥不受限制时起作用。目前我有以下可接受的 HTTP 引荐来源网址:

这允许我的基本 javascript 地图和自动完成表单在 HTML 页面上工作。但是,地理编码是通过访问网站的 python 脚本完成的。com/markers(没有 HTML 页面,如果您访问它,只会显示 JSON 数据)。有什么原因不起作用吗? 403 错误是我在 API 仪表板中看到的,但我在访问网站时收到内部服务器错误消息。com/markers。不受限制时,仪表板显示响应代码 200,我可以看到正确的 JSON 数据。因此,我相信我的代码不是问题所在。

我尝试过的事情:

我已经搜索了这个问题几个小时,但找不到答案,如果我忽略了一些简单的事情,请放过我。

听起来您正在尝试使用 URL 限制方法用于 client-side 网络 api。在这种情况下,站点的 public URL 用于验证。

您想使用 IP 地址验证,用于 server-side 次通话。

以下内容来自https://developers.google.com/maps/faq#keysystem:

API Key: An API key is a unique identifier that you generate using the Google API Console. API keys are generally used with the standard APIs. Premium Plan customers1 typically can choose to use a client ID or an API key. You can choose to use an API key without applying restrictions (called a “generic API key”) or a key with restrictions applied for greater security. APIs in any platform may use a generic API key.

You can optionally add a restriction (for example, IP address) to the API key. Once restricted, a key will only work on platforms that support that type of restriction. Four types of API key restrictions are available:

  • IP addresses (individual servers) - for use with the web service APIs.

  • HTTP referrers (web sites) - for use with the Web APIs.

  • Android app restriction (by package name and fingerprint) - for use with the Android APIs.

  • iOS app restriction (by iOS bundle identifier) - for use with the iOS APIs.

您需要不同的网络服务密钥。您不能对同一密钥同时应用 HTTP 和 IP 限制。您的 Google 地图 Javascript API v3 密钥需要 HTTP 限制,网络服务密钥需要 IP 限制。