【问题标题】:HTTP Error 403 with api_id in accessing Google Maps访问 Google 地图时出现带有 api_id 的 HTTP 错误 403
【发布时间】:2013-09-14 22:40:51
【问题描述】:

我打算在 Python 中使用“googlemaps”包,但遇到了 api_id 问题。

以下代码:

from googlemaps import GoogleMaps
gmaps = GoogleMaps(api_key = 'AIzaSyDjuqAztMNv_TgGdQMdpjMo68x9eNbEl-E')
address = 'Constitution Ave NW & 10th St NW, Washington, DC'
lat, lng = gmaps.address_to_latlng(address)
print lat, lng

错误信息如下:

C:\Users\Linfeng\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.1.0.1371.win-x86_64\lib\urllib2.pyc in http_error_default(self, req, fp, code, msg, hdrs)
    525 class HTTPDefaultErrorHandler(BaseHandler):
    526     def http_error_default(self, req, fp, code, msg, hdrs):
--> 527         raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    528 
    529 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 403: Forbidden

我已经在 Google 上打开了所有与地图有关的服务。

该软件包是否太旧,因此不受 Google 支持?

感谢您的帮助!

一切顺利,

-临风

【问题讨论】:

标签: python google-maps


【解决方案1】:

我遇到了和你一样的错误,但没有找到令人满意的答案,所以我按照在这个网站上找到的这个方法更改了我的脚本来创建我需要的脚本(http://www.portailsig.org/content/python-geocodage-geolocalisation——但是用法语写的)

import urllib, json, csv

def geocode(addr):
    url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false" %   (urllib.quote(addr.replace(' ', '+')))
    data = urllib.urlopen(url).read()
    info = json.loads(data).get("results")[0].get("geometry").get("location")  
    #A little ugly I concede, but I am open to all advices :) '''
    return info

#Open the List file of adresses to look for corresponding lat and lng. 
f = open('list', 'rb')
addresses = f.readlines()
f.close()

#Loop to feed the func with adresses and output the lat & lng.
for a in addresses:
    r = geocode(a)
    print "%s %s" % (r['lat'], r['lng'])

它对我来说很好,除了有时我必须修复的索引错误。

【讨论】:

    【解决方案2】:

    您可以尝试在 python 中使用 geoPy 包。示例代码如下:

    from geopy.geocoders import Nominatim
    geolocator = Nominatim()
    location = geolocator.geocode("Ratainda")
    print((location.latitude, location.longitude))
    

    【讨论】:

      【解决方案3】:

      Api v2 已于 2013 年 9 月 13 日关闭。您的问题是在 14 日提出的 - 所以这可能是一个线索 :) 。 我设法做的只是修改 googlemaps.py 中的 URL,现在它可以正常进行路由了。

      我将 _DIRECTIONS_QUERY_URL(googlemaps.py 的第 165 行)更改为:

      _DIRECTIONS_QUERY_URL = 'http://maps.googleapis.com/maps/api/directions/output?'
      

      而且效果很好。 我也尝试修改第 164 行:_GEOCODE_QUERY_URL = 'http://maps.googleapis.com/maps/api/geocode/output?' 建议 here 但有一些错误,并且 a)我不需要它 b)它已经被 pygeocoder 覆盖,它比 googlemaps.py 更高级

      【讨论】:

      • 我更改了 GoogleMaps 模块的 googlemaps.py 中的 URL,但仍然得到 403。
      猜你喜欢
      • 1970-01-01
      • 2014-01-31
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多