如何在 Ruby 中创建 urn:localhost-catalog SOAP 标头?
我在 php 中有这个(工作)示例:
$usernameToken = new SoapHeaderUsernameToken( $password, $username );
$soapHeaders[] = new SoapHeader("urn:localhost-catalog", 'UsernameToken', $usernameToken);
$client = new SoapClient("my_client_url");
$client->__setSoapHeaders( $soapHeaders );
但我想要一个 Ruby 客户端。我正在使用 savon gem,它可以为您进行身份验证。到目前为止,我有以下方法:
def my_soap_client
client = Savon::Client.new do
wsdl.document = "my_client_url"
end
client.wsse.credentials "usr", "pass" # Adding :digest doesn't seem to help
return client
end
# I make requests like this:
my_soap_client.request :wsdl, "getProductGroups", :parent_id => 1
我可以成功列出可用的操作,但如果我尝试发出请求,结果始终是“(SOAP-ENV:Client)缺少参数”。此外,我是否通过正确的凭据或虚假的凭据并不重要。这就是为什么我认为身份验证失败。
希望有人可以帮忙!
I have this (working) example in php:
$usernameToken = new SoapHeaderUsernameToken( $password, $username );
$soapHeaders[] = new SoapHeader("urn:localhost-catalog", 'UsernameToken', $usernameToken);
$client = new SoapClient("my_client_url");
$client->__setSoapHeaders( $soapHeaders );
But I want a client in Ruby. I'm using the savon gem, which can do authentication for you. So far, I have the following method:
def my_soap_client
client = Savon::Client.new do
wsdl.document = "my_client_url"
end
client.wsse.credentials "usr", "pass" # Adding :digest doesn't seem to help
return client
end
# I make requests like this:
my_soap_client.request :wsdl, "getProductGroups", :parent_id => 1
I can successfully list available actions, but if I try to make a request, the result is always "(SOAP-ENV:Client) Missing parameter". Also, it doesn't matter if I pass correct credentials or fake ones. That is why I believe the authentication fails.
Hope someone can help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Wireshark sniffer 我拦截了您的 PHP 代码发送的 SOAP 请求。它看起来像这样:
在这里您可以看到此代码为 UsernameToken 标头添加了 ns2 命名空间。
然后我拦截了发送 Ruby 代码的 SOAP 请求:
您可以看到区别 - UsernameToken 被包装到 Security 元素中,并且 UsernameToken 的命名空间是“http://docs.oasis-open.org/wss/2004/01/oasis-200401 -wss-wssecurity-secext-1.0.xsd"
您的 PHP 代码使用自定义授权,而 savon 不通过 wsse 属性支持它。我查看了 Savon 源代码,它使用 Akami.wsse 并且在其代码中无法设置命名空间。这意味着您需要手动构建标头。下面是 Ruby 中的代码,它生成您可能需要的 SOAP 请求:
它生成以下 SOAP 请求:
希望这会有所帮助!
Using Wireshark sniffer I intercepted SOAP request that your PHP code sends. It looks like this:
Here you can see that this code adds ns2 namespace for UsernameToken header.
Then I intercepted SOAP request that sends your Ruby code:
You can see the difference - UsernameToken is wrapped into Security element and namespace for UsernameToken is "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
Your PHP code uses custom authorization and savon does not support it via wsse property. I looked into Savon source code, it uses Akami.wsse and in its code there is no possibility to set namespace. This means you need to manually construct headers. Here is the code in Ruby that generates SOAP request like you possibly need:
It generate the following SOAP request:
Hope this helps!
要在 Savon 中设置标头,请使用以下表达式:
也许这对您有帮助。
-英石
to set a header in Savon use the following expression:
perhaps that helps you.
-st