Ruby - Savon web服务身份验证



我试图利用ruby gem Savon连接到propertyware (http://propertyware.com/apidocs/Getting-Started)提供的web服务。我已经成功地通过SoapUI连接到服务,并执行了一个echoString请求。当我尝试通过Ruby做同样的事情时,我得到一个空用户身份验证错误。

这是我在Ruby中尝试过的…

require 'rubygems'
require 'savon'
client = Savon::Client.new do
  wsdl.document = 'http://propertyware.com/pw/services/PWServices?wsdl'
  wsse.credentials 'username', 'pwd'
end
response = client.request :web, :echo_string, :body => {:arg => "Hello world."} 

生成以下xml…

<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:web="http://propertyware.com/pw/services/PWServices" 
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ins0="http://propertyware.com/pw/services/PWServices" 
    xmlns:ins1="http://criteria.soap.propertyware.com" 
    xmlns:ins2="urn:PWServices" 
    xmlns:ins3="http://soap.propertyware.com" 
    xmlns:ins4="http://xml.apache.org/xml-soap">
    <env:Header>
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-1">
                <wsse:Username>user</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pwd</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </env:Header>
    <env:Body>
        <web:echoString>
            <arg>Hello world.</arg>
        </web:echoString>
    </env:Body>
</env:Envelope>

这是SoapUI生成的xml…

<soapenv:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:web="http://webservices" >
   <soapenv:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:Username>user</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pwd</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">R41mCRd+tY+xthhE/YISLQ==</wsse:Nonce>
            <wsu:Created>2011-10-25T09:52:40.220Z</wsu:Created>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <web:echoString soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <arg xsi:type="xsd:string">?</arg>
      </web:echoString>
   </soapenv:Body>
</soapenv:Envelope>
一个明显的区别是SoapUI包含一个nonce键和一个createdAt时间戳。我不知道如何让萨文做到这一点而不去消化真相。

我对web服务的方式不是很在行,任何指导都会很感激。

也-这是当我试图通过savon连接时的响应:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <soapenv:Fault>
            <faultcode xmlns:ns1="http://xml.apache.org/axis/">ns1:Server.Unauthenticated</faultcode>
            <faultstring>User 'null' not authenticated (unknown user)</faultstring>
            <detail>
                <ns2:hostname xmlns:ns2="http://xml.apache.org/axis/">rcpppwwwapt012.realpage.com</ns2:hostname>
            </detail>
        </soapenv:Fault>
    </soapenv:Body>
</soapenv:Envelope>

TIA !鲍勃

尝试将wsse中的摘要设置为true。这将指示Savon将nonce和createdAt添加到请求:

wsse.credentials 'username', 'pwd', :digest

查看该对象的注释获取更多信息

在Savon版本2中,您需要以下内容:

client = Savon.client do
  wsdl 'https://webservice/theservice.wsdl'  
  wsse_auth("user", "pass", :digest)
end

我认为第一步应该是处理错误。

<faultstring>User 'null' not authenticated (unknown user)</faultstring>

您的凭据没有传递给服务。

最新更新