2014-05-12 1 views
8

어떻게 든 HTTParty는 CURL이 제대로 작동하는 경우 401을 반환합니다. 헤더에 토큰을 전달하는 방법을 모릅니다.토큰을 통한 HTTParty 및 권한 부여

작업 (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'하지만 동일한 결과를 시도했다.

답변

20

다음과 같이 작동하도록 관리합니다. 동적 클래스의 헤더를 설정하려는 경우에도 작동

HTTParty.get("http://localhost:3020/api/products", headers: {"Authorization" => "Token token=\"111\""}) 
+0

헤더 부분은 맨 마지막에 인용 누락 던과 브래드 스트리트의 인증 토큰을 얻기위한 것입니다 . : 헤더 : { "Authorization"=> "토큰 토큰 = \"111 \ ""}) –

+0

고정 @BrianKunzig –

+1

실제로는 '111'에 대한 따옴표를 포함 할 필요가 없습니다. – kurenn

1

,이 예는

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 
+0

헤더를 정의 할 수도 있습니다 initialize 메소드에서 base_uri와 같은 방식으로 수행해야합니다. 분명히 initialize 메소드에 전달 된 변수에 의존하지 않는다고 가정하면됩니다. 예를 들어이를 사용하여 API에 대한 Authorization 헤더를 설정할 수 있습니다. – craig1410