引发 ApiClientException(消息)geolocation.exceptions.ApiClientException:请求被拒绝

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

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

获取您的当前位置(不是手动提供 place/address,即如何自动检测您的位置)

我使用有效的 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

希望对您有所帮助!