【问题标题】:Google Geocoding API HTTP Request denied - Ruby谷歌地理编码 API HTTP 请求被拒绝 - 红宝石
【发布时间】:2013-08-26 18:07:15
【问题描述】:

以下(和其他类似的)对 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"
}

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

有什么建议吗?

【问题讨论】:

    标签: ruby-on-rails ruby google-api geocoding


    【解决方案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"
    

    【讨论】:

    • 你是绅士和学者!我从中获得 HTTP 请求代码的 SO question 甚至有一个注释作为警告,猜猜谁忽略了它?这家伙!谢谢拉斯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    相关资源
    最近更新 更多