Google Roads API - HTTP 错误 403:禁止
Posted
技术标签:
【中文标题】Google Roads API - HTTP 错误 403:禁止【英文标题】:Google Roads API - HTTP Error 403: Forbidden 【发布时间】:2021-06-22 14:31:28 【问题描述】:背景和问题:
我正在尝试使用Google Roads API 提取某些道路的速度限制。当我尝试执行网站上描述的basic example 时,我收到错误HTTPError: HTTP Error 403: Forbidden
。我正在使用以下代码:
# Import libraries
import urllib.request as req
# Extract road data
key = 'my_key'
mounted_url = 'https://roads.googleapis.com/v1/speedLimits?path=38.75807927603043,-9.03741754643809|38.6896537,-9.1770515|41.1399289,-8.6094075&key=' + key
response = req.urlopen(mounted_url)
我尝试过的:
我已经检查了API troubleshooting section 和几个类似的问题:
Getting a 403 - Forbidden for Google Service Account 403 Forbidden Error from google API javascript client 403 Forbidden response Google API Google Drive API 403 Forbidden我已经尝试过这些网站上给出的建议:
我的 API 密钥工作正常。如果我使用Google Directions API 提取数据,它工作得非常好:# Extract directions data
mounted_url = 'https://maps.googleapis.com/maps/api/directions/json?origin=Disneyland&destination=Universal+Studios+Hollywood&key=' + key
response = req.urlopen(mounted_url)
我的trial 仍处于活动状态,API 已启用。
额外细节:
这是完整的错误:
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
<ipython-input-3-ffb4fce3ffb5> in <module>
2 key = 'my_key'
3 mounted_url = 'https://roads.googleapis.com/v1/speedLimits?path=38.75807927603043,-9.03741754643809|38.6896537,-9.1770515|41.1399289,-8.6094075&key=' + key
----> 4 response = req.urlopen(mounted_url)
~\Anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
220 else:
221 opener = _opener
--> 222 return opener.open(url, data, timeout)
223
224 def install_opener(opener):
~\Anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout)
529 for processor in self.process_response.get(protocol, []):
530 meth = getattr(processor, meth_name)
--> 531 response = meth(req, response)
532
533 return response
~\Anaconda3\lib\urllib\request.py in http_response(self, request, response)
638 # request was successfully received, understood, and accepted.
639 if not (200 <= code < 300):
--> 640 response = self.parent.error(
641 'http', request, response, code, msg, hdrs)
642
~\Anaconda3\lib\urllib\request.py in error(self, proto, *args)
567 if http_err:
568 args = (dict, 'default', 'http_error_default') + orig_args
--> 569 return self._call_chain(*args)
570
571 # XXX probably also want an abstract factory that knows when it makes
~\Anaconda3\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
500 for handler in handlers:
501 func = getattr(handler, meth_name)
--> 502 result = func(*args)
503 if result is not None:
504 return result
~\Anaconda3\lib\urllib\request.py in http_error_default(self, req, fp, code, msg, hdrs)
647 class HTTPDefaultErrorHandler(BaseHandler):
648 def http_error_default(self, req, fp, code, msg, hdrs):
--> 649 raise HTTPError(req.full_url, code, msg, hdrs, fp)
650
651 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 403: Forbidden
【问题讨论】:
对密钥有什么限制吗? 【参考方案1】:您的代码没有问题。
您提供的代码与我用于执行相同测试的代码完全匹配。
您访问该 API 的权限存在帐户问题。如果您无法在设置中找到问题,建议您直接与 Google 联系。
【讨论】:
谢谢@Emerson,你是对的。这似乎是一个许可证问题。我现在正在努力。【参考方案2】:浏览了一段时间文档后,我发现这是一个许可证问题。速度限制功能似乎是 Roads API 试用版唯一不可用的功能:
来源:
Google Roads API documentation【讨论】:
以上是关于Google Roads API - HTTP 错误 403:禁止的主要内容,如果未能解决你的问题,请参考以下文章
Google Roads API 的 Nearest Roads 函数返回的点数多于给定点,包含重复项
Google Maps 的 Roads API 不适用于 XHR
Google Maps Roads API 返回重复的坐标和 placeIds
如何使用带有 Google Maps Roads API 的坐标列表计算距离