gpt4 book ai didi

ruby-on-rails - HTTParty 和通过 token 授权

转载 作者:行者123 更新时间:2023-12-03 15:16:49 26 4
gpt4 key购买 nike

HTTParty 以某种方式返回 401,其中 CURL 工作正常。不确定如何在 header 中传递 token 。

工作(200):

curl http://localhost:3020/api/products -H 'Authorization: Token token="111"'

不工作(401):
HTTParty.get('http://localhost:3020/api/products', headers: {"Authorization: Token token" => '111'})

我只尝试了 "Authorization" => '111'"token" => '111'但同样的结果。

最佳答案

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

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

关于ruby-on-rails - HTTParty 和通过 token 授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23617812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com