我不知道如何把我的钥匙放进我的请求中,所以它们作为
{"status"=>"400", "message"=>"Token parameter is required."}这是我一直在使用的代码
require 'net/http'
require 'json'
token = 'YiwwVvywLngtPT***************'
url = 'https://www.ncdc.noaa.gov/cdo-web/api/v2/stations?locationid=FIPS:23&limit=5&sortfield=mindate'
uri = URI(url)
response = Net::HTTP.get(uri)
response.authorization = token
puts JSON.parse(response)我尝试了一些我在互联网上发现的不同的东西,但是它们都是错误的。
undefined method `methodname' for #<String:0x00007fd97519abd0>发布于 2019-06-25 14:11:52
根据API文档 (基于您引用的URL ),您需要在名为token的头中提供令牌。
因此,您可能应该尝试下面的一些变化(未经测试的代码):
token = 'YiwwVvywLngtPT***************'
url = 'https://www.ncdc.noaa.gov/cdo-web/api/v2/stations?locationid=FIPS:23&limit=5&sortfield=mindate'
uri = URI(url)
request = Net::HTTP::Get.new(uri)
request['token'] = token
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
end有关Net:HTTP标头的更多信息可以在这个StackOverflow的答案中找到。
另外,如果您没有使用Net::HTTP,请考虑切换到更友好的HTTP客户端,也许是HTTParty。然后,完整的代码如下所示:
require 'httparty'
token = 'YiwwVvywLngtPT***************'
url = 'https://www.ncdc.noaa.gov/cdo-web/api/v2/stations?locationid=FIPS:23&limit=5&sortfield=mindate'
response = HTTParty.get url, headers: { token: token }
puts response.bodyhttps://stackoverflow.com/questions/56754879
复制相似问题