如何使用python googlemaps设置API键
在本教程之后使用Ubuntu 14.04 根据这些说明更新以使用客户端类如何使用python googlemaps设置API键,python,google-maps,Python,Google Maps,在本教程之后使用Ubuntu 14.04 根据这些说明更新以使用客户端类 sudo-apt-get-install-python-pip pip安装-U谷歌地图 python Python 2.7.6(默认值,2015年6月22日,17:58:13) [GCC 4.8.2]关于linux2 有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。 >>>从谷歌地图导入客户端 >>>mapService=Client() 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“/u
sudo-apt-get-install-python-pip
pip安装-U谷歌地图
python
Python 2.7.6(默认值,2015年6月22日,17:58:13)
[GCC 4.8.2]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>从谷歌地图导入客户端
>>>mapService=Client()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/local/lib/python2.7/dist packages/googlemaps/client.py”,第101行,在__
raise VALUERROR(“必须提供API密钥或企业凭据”
ValueError:创建客户端时必须提供API密钥或企业凭据。
我有一个来自Google开发者控制台的API密钥。
如何在pyton中输入API键值
--
编辑:尝试了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)
导入谷歌地图
>>>从谷歌地图导入客户端
>>>mapService=客户端(“AInotqjpmyvA7realBIy-keymA9u0”)
>>>方向=地图服务。方向('Toronto'、'Montreal')
/usr/local/lib/python2.7/dist-packages/urllib3/util/ssl_389;.py:100:InsurePlatformWarning:不存在真正的SSLContext对象。这会阻止urllib3正确配置ssl,并可能导致某些ssl连接失败。有关更多信息,请参阅https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
不安全平台警告
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/local/lib/python2.7/dist packages/googlemaps/directions.py”,第150行,方向
返回客户端。_get(“/maps/api/directions/json”,参数)[“routes”]
文件“/usr/local/lib/python2.7/dist packages/googlemaps/client.py”,第222行,在
结果=自我获得身体(resp)
文件“/usr/local/lib/python2.7/dist-packages/googlemaps/client.py”,第245行,在
正文[“错误消息”])
googlemaps.exceptions.ApiError:请求被拒绝(此API项目无权使用此API。请确保在API控制台中激活此API:了解更多信息:https://code.google.com/apis/console)
您需要根据下面的docstring将密钥传递给
客户端()
函数:
Help on class Client in module googlemaps.client:
class Client(__builtin__.object)
| Performs requests to the Google Maps API web services.
|
| Methods defined here:
|
| __init__(self, key=None, client_id=None, client_secret=None, timeout=None, connect_timeout=None, read_timeout=None, retry_timeout=60, requests_kwargs=None, queries_per_second=10)
| :param key: Maps API key. Required, unless "client_id" and
| "client_secret" are set.
| :type key: string
|
| :param client_id: (for Maps API for Work customers) Your client ID.
| :type client_id: string
|
| :param client_secret: (for Maps API for Work customers) Your client
| secret (base64 encoded).
| :type client_secret: string
|
| :param timeout: Combined connect and read timeout for HTTP requests, in
| seconds. Specify "None" for no timeout.
| :type timeout: int
|
| :param connect_timeout: Connection timeout for HTTP requests, in
| seconds. You should specify read_timeout in addition to this option.
| Note that this requires requests >= 2.4.0.
| :type connect_timeout: int
|
| :param read_timeout: Read timeout for HTTP requests, in
| seconds. You should specify connect_timeout in addition to this
| option. Note that this requires requests >= 2.4.0.
| :type read_timeout: int
|
| :param retry_timeout: Timeout across multiple retriable requests, in
| seconds.
| :type retry_timeout: int
|
| :param queries_per_second: Number of queries per second permitted.
| If the rate limit is reached, the client will sleep for the
| appropriate amount of time before it runs the current query.
| :type queries_per_second: int
|
| :raises ValueError: when either credentials are missing, incomplete
| or invalid.
| :raises NotImplementedError: if connect_timeout and read_timeout are
| used with a version of requests prior to 2.4.0.
|
| :param requests_kwargs: Extra keyword arguments for the requests
| library, which among other things allow for proxy auth to be
| implemented. See the official requests docs for more info:
| http://docs.python-requests.org/en/latest/api/#main-interface
| :type requests_kwargs: dict
所以你可以这样做:
mapService = Client("you_key", "your_client_id", "your_client_secret")
我使用它的方式如下,正如前面所说,您需要在客户机类中传递API密钥 你会注意到,在代理背后工作时,我必须传递更多的参数,但如果你不是在这种情况下,就没有必要了
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的事实。
转到并查看左侧的凭据链接,单击它并添加一个API密钥,您应该进行设置这对您有效吗?我只尝试了密钥,因为我没有客户端id或客户端机密,只有来自Google的API密钥。我得到一个错误,说这不是一个授权的项目。你能正确使用这个库吗?
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)