Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google requires API Key #428

Closed
rgraulus opened this issue Jul 14, 2020 · 10 comments
Closed

Google requires API Key #428

rgraulus opened this issue Jul 14, 2020 · 10 comments

Comments

@rgraulus
Copy link

Does your code supports Google's reverse geocoding since they require the use of an API key? I quickly browsed through your documentation but couldn't find anything on how to use Google API key. Please advise.

@GregNed
Copy link

GregNed commented Jul 30, 2020

Yes, just pass it as the 'key' kwarg. And it applies to all Google's methods in this geocoder. In the example below, I put my key in an environment variable and retrieve via os module:

import os 
key_google = os.environ['GOOGLE_API_KEY']
geocoder.google({'lng': -76.33318, 'lat': 42.50656}, method='reverse', key=key_google)

@rgraulus
Copy link
Author

For some reason I can't get geocoder to work. Here's what I tried:

import os
import geocoder
g = geocoder.google('Mountain View, CA')
print(g.latlng)

None

key_google = os.environ['MY_GOOGLE_KEY']
geocoder.google({'lng': -76.33318, 'lat': 42.50656}, method='reverse', key=key_google)

KeyError Traceback (most recent call last)
ipython-input-20-d2b1c1f1a489> in module>
----> 1 key_google = os.environ['MY_GOOGLE_KEY']
2 geocoder.google({'lng': -76.33318, 'lat': 42.50656}, method='reverse', key=key_google)
~/miniconda3/envs/sandbox/lib/python3.8/os.py in getitem(self, key)
673 except KeyError:
674 # raise KeyError with the original key value
--> 675 raise KeyError(key) from None
676 return self.decodevalue(value)
677

     KeyError: 'MY_GOOGLE_KEY'

@emesterhazy
Copy link

@rgraulus It looks like you don't have the MY_GOOGLE_KEY environ set. What do you see if you type echo $MY_GOOGLE_KEY in bash? If you see nothing, you need to set the variable (i.e. export MY_GOGLE_KEY="yourkeyvalue")

@rgraulus
Copy link
Author

When I type echo $MY_GOOGLE_KEY in the command line it prints out my Google Maps API key. So, the variable is set. However, this did not resolve the issue. Also, I checked my Google credentials and I do have a Google Maps Geocoding API key.

I also tried a few other commands e.g.:
g = geocoder.google('Mountain View, CA')
print(g.latlng)
>>>None

g = geocoder.google([45.15, -75.14], method='reverse')
print(g.city)
print(g.state)
print(g.state_long)
print(g.country)
print(g.country_long)
>>>None
>>>None
>>>None
>>>None

I always get the output None.

@rgraulus
Copy link
Author

Also, I tested my API key by adding it to the following request and it works:

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY

@emesterhazy
Copy link

Based on the trace back you shared (below), it looks like the python process that ran your code does not have access to the MY_GOOGLE_KEY environment variable. You can see this on line 3: ----> 1 key_google = os.environ['MY_GOOGLE_KEY'].

KeyError Traceback (most recent call last)
ipython-input-20-d2b1c1f1a489> in module>
----> 1 key_google = os.environ['MY_GOOGLE_KEY']
2 geocoder.google({'lng': -76.33318, 'lat': 42.50656}, method='reverse', key=key_google)
~/miniconda3/envs/sandbox/lib/python3.8/os.py in getitem(self, key)
673 except KeyError:
674 # raise KeyError with the original key value
--> 675 raise KeyError(key) from None
676 return self.decodevalue(value)
677

Can you try running your code again from the same shell where you confirmed that the environment variable is set?

Regarding the None output, I'm pretty sure that Google requires an API key now for all requests. If the key isn't set up properly, you probably will not get a useful response from Google's API.

@rgraulus
Copy link
Author

Great, it now works!

Two more questions:

  1. Can you also enter multiple lat/lons in a single request? If yes, what is the syntax?
  2. Can you write the output directly into a csv file?

Thanks!

@emesterhazy
Copy link

I haven't used this project for a while, so I can't comment on your first point. My guess is that you can't, but you can review the documentation and or source code for the Google api portion of the project to confirm. Regarding your second question, you can definitely write the output (and whatever else you want) to a CSV. Checkout Python's built in csv module: https://docs.python.org/3/library/csv.html

@rgraulus
Copy link
Author

rgraulus commented Aug 4, 2020

Thanks!

@rgraulus rgraulus closed this as completed Aug 4, 2020
orcahmlee added a commit to orcahmlee/geocoder that referenced this issue Aug 26, 2020
@rgraulus rgraulus reopened this Sep 22, 2020
@rgraulus
Copy link
Author

Hi,

I have the same issue pop up again!

Since I get the same None output as before as well as KeyError: 'MY_GOOGLE_KEY' I assume this has to do with the PATH again...Not sure why??

I added the directory of the conda env where geocoder is installed to the .bashrc file:
export PATH=$PATH:/home/rik/miniconda3/envs/sandbox

But this does not solve the problem.

What am I doing wrong?

Regards

Rik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants