Skip to main content
 首页 » 编程设计

python之请求 google directions api 错误地要求传感器

2025年05月04日9itcoder

我正在用 Python 请求 Google 的指示,我收到了以下 JSON 格式的响应:

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

我打的url是http://maps.googleapis.com/maps/api/directions/json我发送的数据如下:

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

注意传感器属性。相应的属性是 London、Karachi、None、'false' 和 None。 我发送请求的方式是:

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

有谁知道为什么这行得通?围绕这个问题的所有搜索都让我发现人们在他们的脚本标签中遗漏了它。

请您参考如下方法:

您需要将参数放在URL中; urllib2.Request() 的第二个参数用于 POST 数据,但您需要一个 GET 请求:

req = urllib2.Request('{}?{}'.format(google.urls['directions'], urllib.urlencode(data)))