2

背景和问题:

我正在尝试使用Google Roads API提取某些道路的速度限制。当我尝试执行网站上描述的基本示例时,出现错误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 故障排除部分和几个类似的问题:

我已经尝试过这些网站上给出的建议:

# 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)

额外细节:

这是完整的错误:

---------------------------------------------------------------------------
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
4

2 回答 2

1

您的代码没有问题。

您提供的代码与我用来执行相同测试的代码完全匹配。

您访问该 API 的权限存在帐户问题。如果您无法在设置中找到问题,我建议您直接与 Google 联系。

于 2021-06-22T14:51:57.370 回答
0

在浏览了一段时间的文档后,我发现这是一个许可证问题。速度限制功能似乎是 Roads API 试用版唯一不可用的功能:

执照

资料来源:

于 2021-07-04T05:14:05.907 回答