具有摘要式身份验证的 HTTParty
我想使用 Ruby On Rails 2.3.8 和 HTTParty gem 连接到 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_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
我想做的是调用 API 的一个名为 Put 的方法,并将某些参数分组在 data
参数中。
执行此方法后,我收到 401 Unauthorized
错误消息。
我做错了什么?这是我第一次尝试做这样的事情。
I would like to connect to an API using Ruby On Rails 2.3.8 and HTTParty gem.
My model is the following:
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
What I would like to do is to call a method of the API called Put, with certain parameters grouped within data
parameter.
After executing this method I'm getting a 401 Unauthorized
error message.
What am I doing wrong? This is the first time I'm trying to do something like this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是哪个版本的 HTTParty?您是否尝试过使用 Github 上的最新版本?不久前,
0.7.3
版本中修复了一些与摘要身份验证安全性相关的问题。如果这不起作用,则可能是您尝试与之通信的服务器未正确遵循协议。我以前也遇到过这种情况,必须给 HTTParty 打补丁才能正确登录。我将把我使用的补丁放在这里,以防它对您有用...
所做的更改是发送 NC 1 而不是 NC 0,并且还执行 GET 请求,而不是 setup_digest_auth 中的 HEAD 请求
What version of HTTParty are you using, and have you tried using the very latest version from Github? There were some fixes to do with digest auth security a little while ago in version
0.7.3
.If that doesn't work it could be that the server you're attempting to talk to isn't following protocol correctly. I've had this happen before, had to monkey patch HTTParty to get it to login correctly. I'll put the patch I used here in-case it works for you...
The changes made were to send NC 1 and not NC 0, and also to do a GET request, rather than a HEAD request in
setup_digest_auth