Domanda

I am requesting directions from Google in Python, and I get the following response in JSON:

{
error_message: "The 'sensor' parameter specified in the request must be set to either 'true' or 'false'.",
routes: [ ],
status: "REQUEST_DENIED"
}

The url I hit is http://maps.googleapis.com/maps/api/directions/json and the data I send is below:

data = {
    'origin': origin,
    'destination': destination,
    'mode': mode,
    'sensor': 'false',
    'departure_time': departure_time
}

Note the sensor attribute. The respective attributes are London, Karachi, None, 'false' and None. The way I send the request is:

req = urllib2.Request(google.urls['directions'], urllib.urlencode(data))
r = urllib2.urlopen(req)
return HttpResponse(r, content_type="application/json")

Does anyone know why this doenst work? All the searches around this problem lead me to people missing it out of their script tags.

È stato utile?

Soluzione

You need to put the parameters in the URL; the second argument to urllib2.Request() is for POST data instead, but you want a GET request here:

req = urllib2.Request('{}?{}'.format(google.urls['directions'], urllib.urlencode(data)))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top