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)
如果您想动态设置类的标头,这也适用,此示例用于获取 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)
| 归档时间: |
|
| 查看次数: |
10582 次 |
| 最近记录: |