简体   繁体   English

如何从 webhook Ruby on Rails 获取不记名令牌

[英]How to get the bearer token from a webhook Ruby on Rails

I receive datas from webhook and I try to get the Authorization Bearer token that is in the headers我从 webhook 接收数据,并尝试获取标头中的Authorization Bearer token

I tried :我试过 :

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

Also :还 :

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

-> I have the ERROR -> 我有错误

And :和 :

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

-> It returns nil -> 它返回零

Turns out that the solution was :事实证明,解决方案是:

bearer_token = request.headers["Authorization"]

Thanks all for your help !感谢你的帮助 !

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

The library you use to get the response seems to parse the response on its own, you don't need to call JSON#parse again.您用于获取响应的库似乎会自行解析响应,您无需再次调用JSON#parse The below should work.下面应该工作。

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

在此处输入图片说明

..... .....

I think you can get Authorization Bearer token like :-我认为您可以获得授权承载令牌,例如:-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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