llinfeng
llinfeng

Reputation: 49

HTTP Error 403 with api_id in accessing Google Maps

I am planning to use the package "googlemaps" in Python and had trouble with the api_id.

The following codes:

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

The error message is as follows:

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

I've opened all the services on Google that has to do with the maps already.

Is that package too old and therefore were not supported by Google?

Thanks for your help!

All the best,

-Linfeng

Upvotes: 2

Views: 3064

Answers (3)

user1829708
user1829708

Reputation: 73

You can try using geoPy package in python. Sample code is as below:

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

Upvotes: 1

Api v2 was closed down on sept 13, 2013. Your question was asked on 14th - so that migh be a clue :) . What I managed to do is simply modify URLs in googlemaps.py and now it's working OK for routing.

I changed _DIRECTIONS_QUERY_URL (line 165 of googlemaps.py) to:

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

And it worked fine. Also I tried modiying line 164: _GEOCODE_QUERY_URL = 'http://maps.googleapis.com/maps/api/geocode/output?' as suggested here but there's some error, and a) I don't need it anyway b) it's already covered by pygeocoder which is a bit more advanced that googlemaps.py

Upvotes: 0

lkostka
lkostka

Reputation: 751

I faced the same error as you, and didn't find a satisfying answer, so I changed my script following this method found on this site to create the script I needed (http://www.portailsig.org/content/python-geocodage-geolocalisation -- However written in french)

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'])

It works fine for me, except index bug sometimes that I have to fix.

Upvotes: 3

Related Questions