gpt4 book ai didi

python - 如何使用 python googlemaps 设置 API key

转载 作者:行者123 更新时间:2023-11-30 23:03:42 24 4
gpt4 key购买 nike

按照本教程使用 Ubuntu 14.04 https://www.youtube.com/watch?v=PzkHU5GM8_E

按这些说明更新以使用 Client 类 https://stackoverflow.com/a/27294555/5619635

sudo apt-get install python-pip
pip install -U googlemaps
python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from googlemaps import Client
>>> mapService = Client()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/googlemaps/client.py", line 101, in __init__
raise ValueError("Must provide API key or enterprise credentials "
ValueError: Must provide API key or enterprise credentials when creating client.

我有来自 Google 开发者控制台的 API key 。如何在 pyton 中获取 API key 值?

--

编辑:尝试了 skycrew 提供的解决方案,但现在收到错误消息,指出 googlemaps 不是受支持的 API 项目。

>>> import googlemaps
>>> from googlemaps import Client
>>> mapService = Client("AInotqjpmyvA7realBIy-keymA9u0")
>>> directions = mapService.directions('Toronto', 'Montreal')
/usr/local/lib/python2.7/dist-packages/urllib3/util/ssl_.py:100: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/googlemaps/directions.py", line 150, in directions
return client._get("/maps/api/directions/json", params)["routes"]
File "/usr/local/lib/python2.7/dist-packages/googlemaps/client.py", line 222, in _get
result = self._get_body(resp)
File "/usr/local/lib/python2.7/dist-packages/googlemaps/client.py", line 245, in _get_body
body["error_message"])
googlemaps.exceptions.ApiError: REQUEST_DENIED (This API project is not authorized to use this API. Please ensure that this API is activated in the APIs Console: Learn more: https://code.google.com/apis/console)

最佳答案

我的使用方式如下,如skycrew说你需要在客户端类中传递你的 API key 。

您会注意到,在代理后面工作时,我必须传递更多参数,但如果您不处于这种情况,则没有必要。

import googlemaps
GOOGLE_MAP_KEY = 'yourkey'
PROXY = {'proxies': {"https": "https://user:password@proxyurl:proxyport/"}}
gmaps = googlemaps.Client(GOOGLE_MAP_KEY, requests_kwargs=PROXY)
results = gmaps.geocode(address=search)

现在您的错误似乎来自于您没有正确注册 API。前往https://console.developers.google.com并查看左侧的“凭据”链接,单击它并添加 API key ,您应该已设置完毕

关于python - 如何使用 python googlemaps 设置 API key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33991473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com