raise ApiClientException(message) geolocation.exceptions.ApiClientException: Request was denied

余生颓废 提交于 2021-02-11 13:35:39

问题


from geolocation.main import GoogleMaps
if __name__=="__main__":
    address="hyderabad"
    gmap=GoogleMaps(api_key='MY_API_KEY')
    locate=gmap.search(location =address)
    print(locate.all())
    my_location = locate.first()
    print(my_location.city)
    print(my_location.route)
    print(my_location.street_number)
    print(my_location.postal_code)
    for administrative_area in my_location.administrative_area:
        print("%s: %s" % (administrative_area.area_type, administrative_area.name))
    print(my_location.country)
    print(my_location.country_shortcut)
    print(my_location.formatted_address)
    print(my_location.lat)
    print(my_location.lng)

this is the code for finding your current location but I'm getting this error i.e

Traceback (most recent call last):
  File "/home/student/Desktop/currentlocation.py", line 5, in <module>
    location=gmap.search(location=address)
  File "/usr/local/lib/python3.6/dist-packages/geolocation/main.py", line 18, in search
    return self.geocode.search(location, lat, lng)
  File "/usr/local/lib/python3.6/dist-packages/geolocation/geocode/main.py", line 48, in search
    data = self.client.get_data(address=address, latitude=latitude, longitude=longitude)
  File "/usr/local/lib/python3.6/dist-packages/geolocation/geocode/client.py", line 40, in get_data
    return self.send_data(self.API_URL, self.query_parameters)
  File "/usr/local/lib/python3.6/dist-packages/geolocation/client.py", line 23, in send_data
    self.validator(response)
  File "/usr/local/lib/python3.6/dist-packages/geolocation/validators.py", line 28, in __init__
    self.validation(response)
  File "/usr/local/lib/python3.6/dist-packages/geolocation/validators.py", line 55, in validation
    self.validate_google_status(response)
  File "/usr/local/lib/python3.6/dist-packages/geolocation/validators.py", line 46, in validate_google_status
    raise ApiClientException(message)
geolocation.exceptions.ApiClientException: Request was denied.

Help me to get out of this and suggest me how to get your current location(not giving place/address manually i.e how to detect your location automatically) using python


回答1:


I get this error as well with my valid API key, even when I use that library's own example code; note that this library hasn't been updated in 4 years so that's likely the issue here.

If you need to geocode using python, I recommend you use the up-to-date and maintained Python Client for Google Maps Web Services.

Here's a code sample:

import googlemaps

gmaps = googlemaps.Client(key='YOUR_API_KEY')

geocode_result = gmaps.geocode("hyderabad")

print(geocode_result)

Output:

[{'address_components': [{'long_name': 'Hyderabad', 'short_name': 'Hyderabad', 'types': ['locality', 'political']}, {'long_name': 'Telangana', 'short_name': 'Telangana', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'India', 'short_name': 'IN', 'types': ['country', 'political']}], 'formatted_address': 'Hyderabad, Telangana, India', 'geometry': {'bounds': {
    'northeast': {'lat': 17.6078088, 'lng': 78.6561694}, 'southwest': {'lat': 17.2168886, 'lng': 78.1599217}}, 'location': {'lat': 17.385044, 'lng': 78.486671}, 'location_type': 'APPROXIMATE', 'viewport': {'northeast': {'lat': 17.6078088, 'lng': 78.6561694}, 'southwest': {'lat': 17.2168886, 'lng': 78.1599217}}}, 'place_id': 'ChIJx9Lr6tqZyzsRwvu6koO3k64', 'types': ['locality', 'political']}]

Make sure you're using a valid API key, i.e. you have to enable billing and the Geocoding API on your project for it to work. See Google's get started guide.

Hope this helps!



来源:https://stackoverflow.com/questions/59414546/raise-apiclientexceptionmessage-geolocation-exceptions-apiclientexception-req

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!