如何从Webhook Ruby on Rails获取承载令牌

时间:2019-12-30 09:59:27

标签: ruby-on-rails ruby webhooks bearer-token

我从webhook接收数据,并尝试获取标题中的Authorization Bearer token

我尝试过:

data = JSON.parse(response.body)
puts "TOKEN " + data['csrf-token']['content']

也:

if headers['Authorization'].present?
  puts " HEADER " + headers['Authorization'].split(' ').last
 else
   puts "ERROR"
end

->我遇到了错误

然后:

data = response.body
puts "TOKEN " + data['csrf-token']['content']

->返回nil

原来的解决方法是:

bearer_token = request.headers["Authorization"]

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

data = JSON.parse(response.body)
#⇒ JSON::ParserError (767: unexpected token ...

您用于获取响应的库似乎可以自动解析响应,您无需再次调用JSON#parse。以下应该可以。

data = response.body
puts "TOKEN " + data['csrf-token']['content']

答案 1 :(得分:0)

https://github.com/django/django/tree/master/django/contrib/auth/locale

.....

我认为您可以获得授权承载令牌,例如:-

if headers['Authorization'].present?
  return headers['Authorization'].split(' ').last
else
  errors.add(:token, 'Missing token')
end