问题描述
我有一个 API 密钥.这是一个浏览器应用程序的密钥(带有引用程序).它工作正常,但是当我尝试在本地开发服务器上使用它时我没有获得授权.我使用 MAMP,我的本地 URL 如下所示:http://mysite.dev
.
I have an API key. It's a "Key for browser apps (with referers). It works fine, but I'm not authorized when I try to use it on my local development server. I use MAMP and my local URL looks like this: http://mysite.dev
.
在推荐人"部分,我有:
In "Referers" section I have:
mysite.com/*
mysite.dev/*
生产版 (.com) 运行良好,所以我很确定我的语法是正确的.但是无论我对本地版本尝试什么,我都会收到来自 Google 的授权错误弹出窗口,告诉我:
The production one (.com) works fine, so I'm pretty sure my syntax is correct. But no matter what I try for the local version, I get the authorization error popup from Google telling me:
Google 已禁止为此应用程序使用 Maps API.这提供的密钥不是有效的 Google API 密钥,或者未经授权用于本网站上的 Google Maps Javascript API v3.如果你是此应用程序的所有者,您可以了解如何获取有效密钥这里:https://developers.google.com/maps/documentation/javascript/tutorial#api_key
当然有办法让它工作!它是什么?
Surely there is a way to get this working! What is it?
推荐答案
更新:
自 2016 年 6 月 22 日起,Google Maps V3 不再支持无密钥访问(任何不包含 API 密钥的请求).
As of June 22, 2016 Google Maps V3 no longer support keyless access (any request that doesn't include an API key).
您可以注册密钥:https://developers.google.com/maps/documentation/javascript/get-api-key
并将其添加到您的 URL 中:
and add it to your URL :
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY" type="text/javascript"></script>
我的应用程序遇到了类似的问题.我使用没有客户端密钥的 url 进行测试,并在将代码放入生产服务器之前添加密钥.这不仅仅是一种解决方法,而且我假设您对本地测试的使用率会很低.
I have faced a similar issue with my application. I use the url without the client key for testing purposes and add the key before putting the code onto the production server. This is a workaround more than a solution and I am assuming that your usage for local testing will be low.
测试服务器
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?sensor=SET_TO_TRUE_OR_FALSE">
</script>
生产服务器
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=SET_TO_TRUE_OR_FALSE">
</script>
网址:https://developers.google.com/maps/documentation/javascript/示例/
如果您查看以下站点并转到基本地图示例,您会发现这些示例不使用密钥.这是映射的 v2 和 v3 之间的差异之一,即密钥不是必需的.
If you check the following site and go to the basic map example you will find that the examples do not use a key. This was one of the differences between v2 and v3 of the maps that the key is not mandatory.
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
请记住,省略密钥属于免费的 Google Maps API 许可范围.如果您需要跟踪使用情况,您必须至少提供密钥.如果您需要更多流量,则需要提供您的客户端 ID (Google Maps for Work).
Keep in mind that omitting the key falls under the free Google Maps API licensing. If you need to track usage, you must supply at least the key. If you need more traffic, you need to supply your client ID (Google Maps for Work).
https://developers.google.com/maps/licensing
这篇关于Google Maps v3 API 密钥不适用于本地测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!