1

当我将其粘贴到浏览器中时,对 Google 地理编码 API 的以下(和其他类似的)HTTP 请求正确显示

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

当我在 Ruby 中执行以下操作时

url = URI.parse('http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}
puts res.body

我明白了

{
"results" : [],
"status" : "REQUEST_DENIED"
}

例如,这与我在未设置“传感器”属性时遇到的错误相同,但这不是问题。

有什么建议么?

4

1 回答 1

1

您需要使用#request_uri而不是#path,否则您的查询参数将不会被包括在内:

url = URI.parse('http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false')

url.path
# => "/maps/api/geocode/json"

url.request_uri
# => "/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"
于 2013-08-26T18:17:48.300 回答