HTTParty和通过令牌授权

oli*_*art 15 curl ruby-on-rails httparty access-token

不知何故,HTTParty返回401 CURL正常工作.不确定如何在标头中传递令牌.

工作(200):

curl http://localhost:3020/api/products -H 'Authorization: Token token="111"'
Run Code Online (Sandbox Code Playgroud)

不工作(401):

HTTParty.get('http://localhost:3020/api/products', headers: {"Authorization: Token token" => '111'})
Run Code Online (Sandbox Code Playgroud)

我曾尝试只用"Authorization" => '111'"token" => '111',但相同的结果.

oli*_*art 31

管理以使其工作如下.

HTTParty.get("http://localhost:3020/api/products", headers: {"Authorization" => "Token token=\"111\""})
Run Code Online (Sandbox Code Playgroud)

  • 它实际上应该是 `headers: { "Authorization" => "Bearer your_token_here" }` (6认同)
  • 实际上你不必包含`111`的引号 (2认同)

Con*_*nor 6

如果您想动态设置类的标头,这也适用,此示例用于获取 Dun and Bradstreet 的授权令牌

require 'httparty'

require 'certified'

class DnbAuth


  include HTTParty

  debug_output $stdout

  base_uri "https://maxcvservices.dnb.com/rest/Authentication"


  def initialize(ct,u,p)

    self.class.headers 'Content-type' =>  "#{ct}"

    self.class.headers 'x-dnb-user' => "#{u}"

    self.class.headers 'x-dnb-pwd'=> "#{p}"

  end


  def token()


    response = self.class.post("/")



  end





end


ct = 'text/xml'
u = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
p = 'xxxxxx'

xx = DnbAuth.new(ct,u,p)

puts xx.token.message
Run Code Online (Sandbox Code Playgroud)