Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 462
k9b Avatar asked Jun 23 '14 23:06

k9b


People also ask

Which is the example of authentication token?

An authentication token (security token) is a “trusted device” used to access an electronically restricted resource (usually an application or a corporate network). It can be seen as an electronic key that enables a user to authenticate and prove his identity by storing some sort of personal information.


1 Answers

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'})
like image 134
zishe Avatar answered Oct 07 '22 07:10

zishe