【问题标题】:raise ApiClientException(message) geolocation.exceptions.ApiClientException: Request was deniedraise ApiClientException(message) geolocation.exceptions.ApiClientException: 请求被拒绝
【发布时间】:2019-12-19 17:42:50
【问题描述】:
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)

这是查找您当前位置的代码,但我收到此错误,即

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.

帮助我摆脱困境并建议我如何使用 python 获取您当前的位置(不手动提供地点/地址,即如何自动检测您的位置)

【问题讨论】:

    标签: python google-maps google-geocoding-api


    【解决方案1】:

    我的有效 API 密钥也会出现此错误,即使我使用该库自己的示例代码也是如此;请注意,这个库已经 4 年没有更新了,所以这可能是这里的问题。

    如果您需要使用 python 进行地理编码,我建议您使用最新且维护的Python Client for Google Maps Web Services

    这是一个代码示例:

    import googlemaps
    
    gmaps = googlemaps.Client(key='YOUR_API_KEY')
    
    geocode_result = gmaps.geocode("hyderabad")
    
    print(geocode_result)
    

    输出:

    [{'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']}]
    

    确保您使用的是有效的 API 密钥,即您必须在项目中启用计费和地理编码 API 才能使其正常工作。请参阅 Google 的 get started guide

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 2018-11-30
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 2019-12-06
      相关资源
      最近更新 更多