简体   繁体   中英

Token authentication

Simple code

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

Just wondering how to put an authentication token in php cURL i do it like this

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

Wondering how to do it for Ruby.

You can simply add header:

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

or shorter:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM