Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对谷歌时区API的Ruby请求返回EconReset,但可以从浏览器中工作_Ruby_Json_Https_Google Api - Fatal编程技术网

对谷歌时区API的Ruby请求返回EconReset,但可以从浏览器中工作

对谷歌时区API的Ruby请求返回EconReset,但可以从浏览器中工作,ruby,json,https,google-api,Ruby,Json,Https,Google Api,我的RoR web应用程序需要向Google时区API发送请求,因此我向控制器添加了以下Ruby代码: 300: url = "https://maps.googleapis.com/maps/api/timezone/json?sensor=false&location=34.0617109,-118.4017053&timestamp=16071" 301: uri = URI.parse(url) 302: raw_response = Net::HTTP

我的RoR web应用程序需要向Google时区API发送请求,因此我向控制器添加了以下Ruby代码:

300:    url = "https://maps.googleapis.com/maps/api/timezone/json?sensor=false&location=34.0617109,-118.4017053&timestamp=16071"
301:    uri = URI.parse(url)
302:    raw_response = Net::HTTP.get(uri)
303:    parsed_response = JSON(raw_response)
但它失败了,我在服务器上看到以下日志

Errno::ECONNRESET (Connection reset by peer):
  app/controllers/projects_controller.rb:302:in `get_rate'

Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.5ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (5.2ms)
如果我复制上面的url并将其粘贴到我的浏览器(Safari),一切正常,我会得到以下预期响应

{
   "dstOffset" : 0,
   "rawOffset" : -28800,
   "status" : "OK",
   "timeZoneId" : "America/Los_Angeles",
   "timeZoneName" : "Pacific Standard Time"
}
同一个web应用程序向Google geocode API发送以下请求,所有请求都能正常工作:

url = "http://maps.googleapis.com/maps/api/geocode/json?address=90212&sensor=false"
uri = URI.parse(url)
raw_response = Net::HTTP.get(uri)
parsed_response = JSON(raw_response)

任何人都可以告诉我,我做错了什么?

回答我自己的问题,让其他人从中学习

经过大量研究和尝试,以下是对我有效的解决方案:

1-我需要一个使用谷歌时区API的密钥。当您使用浏览器时,请求可以正常工作,但是为了使用API,您需要一个密钥,您可以从

2-这就是我如何更改Ruby代码以使其正常工作的方法

url = "https://maps.googleapis.com/maps/api/timezone/json?sensor=false&location=34.0617109,-118.4017053&timestamp=16071&key=<google_timezone_api_key_here>"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
raw_response = http.request request
parsed_response = JSON(raw_response)
url=”https://maps.googleapis.com/maps/api/timezone/json?sensor=false&location=34.0617109,-118.4017053×tamp=16071&key=
uri=uri.parse(url)
http=Net::http.new(uri.host,uri.port)
http.use_ssl=true
request=Net::HTTP::Get.new(uri.request\u uri)
原始响应=http.request
解析的_响应=JSON(原始_响应)
对我来说,奇怪的是,与http请求不同,我正在寻找的https响应(如果我从浏览器发送请求,则返回的响应)位于raw_response.body中,而不是解析的_响应中。 我希望其他人能解释为什么