komputes
komputes

Reputation: 139

How do I setting the API key using python googlemaps

Using Ubuntu 14.04 following this tutorial https://www.youtube.com/watch?v=PzkHU5GM8_E

Updated by these instructions to use the Client class 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.

I have an API key from the Google developer console. How do I the API key value in pyton?

--

Edit: Tried the solution provided by skycrew but now getting the error that googlemaps is not a supported API project.

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

Upvotes: 1

Views: 7683

Answers (2)

euri10
euri10

Reputation: 2626

the way I use it is the following, as skycrew said you need to pass your API key in the client class.

You'll notice here that being at work behind a proxy I had to pass some more arguments but it's not necessary if you're not in this situation.

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)

now your error seems to come from the fact you didn't register your API properly. Go to https://console.developers.google.com and see the Credentials link on the left, click on it and add an API key, you should be set

Upvotes: 2

skycrew
skycrew

Reputation: 918

You need to pass the key to Client() function as per docstring below:

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

So you can do like this:

mapService = Client("you_key", "your_client_id", "your_client_secret")

Upvotes: 1

Related Questions