令牌认证

时间:2014-06-23 23:51:38

标签: ruby api rest authentication

简单代码

require 'net/http'

url = URI.parse('get json/other data here [link]')
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}
puts res.body

只是想知道如何在php cURL中放置身份验证令牌,我就这样做了

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer xxx')); //Bearer token for authentication

想知道如何为Ruby做这件事。

1 个答案:

答案 0 :(得分:11)

您只需添加标题:

即可
req = Net::HTTP::Get.new(url.to_s)
req['Authorization'] = "Bearer xxx"

或更短:

request = Net::HTTP::Get.new(url.to_s, {'Authorization' => 'Bearer vF9dft4qmT'})