python - Google Roads API - HTTP 错误 403 : Forbidden

标签 python api google-api request google-roads-api

背景和问题:

我正在尝试使用 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和几个类似的问题:

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

# 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

最佳答案

您的代码没有问题。

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

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

关于python - Google Roads API - HTTP 错误 403 : Forbidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68085471/

相关文章:

javascript - 方法在 API 请求上使用哪个 IP(用户或服务器)?

Spring Boot + Spring Security + Spring OAuth2 + Google 登录

google-chrome-extension - 错误 403 : access_denied The developer hasn’t given you access to this app despite a new project being created

android - 远程媒体播放器 HTTP 响应处理

python - 动态拆分 DataFrame 的列并将其存储为新列

python - 在列表理解中进行分配

python - 如何使用 PyYAML 创建日期时间对象

Python 测试 - 重置所有模拟?

python - 使用 python 中的请求循环分页 REST API

arrays - WordPress 插件设置类中的动态数组选项