Ruby on rails 具有摘要身份验证的HTTParty
我想使用RubyonRails2.3.8和HttpPartyGem连接到一个API 我的模型如下:Ruby on rails 具有摘要身份验证的HTTParty,ruby-on-rails,httparty,Ruby On Rails,Httparty,我想使用RubyonRails2.3.8和HttpPartyGem连接到一个API 我的模型如下: class Onnion < ActiveRecord::Base require 'net/http' include HTTParty base_uri 'http://myapiurl.com' digest_auth 'user', 'password' disable_rails_query_string_format def self.create_r
class Onnion < ActiveRecord::Base
require 'net/http'
include HTTParty
base_uri 'http://myapiurl.com'
digest_auth 'user', 'password'
disable_rails_query_string_format
def self.create_rma(order)
put('/orders/rma', :query => {:action => 'put', :data => {:api_key => 'user', :onnion_order_id => order.id, :customer_rma => order.saving.profile.user.id, :comments => ''}})
end
end
classonnion{:action=>'put',:data=>{:api\u key=>'user',:onnion\u order\u id=>order.id,:customer\u rma=>order.saving.profile.user.id,:comments=>'})
终止
终止
我想做的是调用一个名为Put的API方法,将某些参数分组在data
parameter中
执行此方法后,我收到一条401未经授权的错误消息
我做错了什么?这是我第一次尝试这样做。您使用的是什么版本的HTTParty,您是否尝试过使用Github的最新版本?不久前,在版本0.7.3
中对摘要身份验证安全性进行了一些修复
如果这不起作用,则可能是您尝试与之对话的服务器没有正确遵循协议。我以前也有过这样的情况,我必须对HttpParty进行修补以使其正确登录。我会把我用过的补丁放在这里,以防对你有用
module Net
module HTTPHeader
class DigestAuthenticator
# use NC = 1 instead of 0
def authorization_header
@cnonce = md5(random)
header = [%Q(Digest username="#{@username}"),
%Q(realm="#{@response['realm']}"),
%Q(nonce="#{@response['nonce']}"),
%Q(uri="#{@path}"),
%Q(response="#{request_digest}")]
[%Q(cnonce="#{@cnonce}"),
%Q(opaque="#{@response['opaque']}"),
%Q(qop="#{@response['qop']}"),
%Q(nc="1")].each { |field| header << field } if qop_present?
header
end
private
def request_digest
a = [md5(a1), @response['nonce'], md5(a2)]
a.insert(2, "1", @cnonce, @response['qop']) if qop_present?
md5(a.join(":"))
end
end
end
end
module HTTParty
class Request
def setup_digest_auth
# issue a get instead of a head request
res = http.get(uri.request_uri, options[:headers]||{})
if res['www-authenticate'] != nil && res['www-authenticate'].length > 0
@raw_request.digest_auth(username, password, res)
end
end
end
end
模块网络
模块HTTPHeader
类验证器
#使用NC=1而不是0
def授权头
@cnonce=md5(随机)
header=[%Q(摘要用户名=“#{@username}”),
%Q(realm=“#{@response['realm']}”),
%Q(nonce=“#{@response['nonce']}”),
%Q(uri=“#{@path}”),
%Q(response=“#{request_digest}”)]
[%Q(cnonce=“#{@cnonce}”),
%Q(不透明=“#{@response['opaque']}”),
%Q(qop=“#{@response['qop']}”),
%Q(nc=“1”)])。每个{字段|头0
@原始请求摘要认证(用户名、密码、资源)
终止
终止
终止
终止
所做的更改是发送NC 1而不是NC 0,并且在setup\u digest\u auth
中执行GET请求,而不是HEAD请求。我使用的是版本0.7.8。因此,我认为这应该已经修复。