开发者

Trying to connect to a "digest authentication" webservice using HTTParty or Net:HTTP (or etc)

开发者 https://www.devze.com 2022-12-15 18:05 出处:网络
I have been trying to connect to a web service that is using digest authentication. I am able to connect in Safari using user:password@service.site.com/endpoint

I have been trying to connect to a web service that is using digest authentication.

I am able to connect in Safari using user:password@service.site.com/endpoint

I have tried in Ruby and Rails to connect using HTTParty and Net:HTTP using the "basic"auth" options, but have not had any luck.

Wondering if the HTTParty/Net:HTTP "basic_auth开发者_StackOverflow" option is not going to be compatible with a "digest auth" service?

If not, is there another way that I might connect?


HTTParty basic auth is apparently not compatible with digest_auth. I found this Net:HTTP extension: https://codesnippets.joyent.com/posts/show/1075 and am writing a method to handle this, with the help of the Crack gem http://github.com/jnunemaker/crack:

 def self.decode vin
    url = URI.parse(APP_CONFIG[:vinlink_url])
    Net::HTTP.start(url.host) do |http|
      res = http.head(url.request_uri)
      req = Net::HTTP::Get.new("/report?type=basic&vin=#{vin}")
      req.digest_auth(APP_CONFIG[:vinlink_login], APP_CONFIG[:vinlink_password], res)
      @response = http.request(req)
    end
    if @response.code == "200"
      hash = Crack::XML.parse(@response.body).recursive_downcase_keys!.recursive_symbolize_keys!
    end
  end  


Wasn't able to get to the codesnippets link given above today, but code is also available here https://gist.github.com/73102. I've used this successfully for digest authentication, but ran into problems with multiple request, getting 'Stale client nonce' errors - resolved by generating a new nonce within the digest_auth function each time it was called. Didn't find much on that when I looked, so hope this helps someone.

0

精彩评论

暂无评论...
验证码 换一张
取 消