Oleg B
Oleg B

Reputation: 85

Rails Savon SOAP Authentication

I am developing on Rails 3 and using Savon(savonrb.com)

  1. I need to connect to Web Services via SOAP
  2. Web Services require authentication

I can connect but can't get authonticated...

I used SOAP UI for testing & all works fine.

Here how my Rails code looks like:

client = Savon::Client.new do
  wsdl.document = "http://services.blahblah.com/Service.asmx?WSDL"
end

client.wsse.credentials "username", "password", :digest

if response.success?

  @soap_status = 'Connected'
  @data = response.to_array(:get_brochure_response, :error_message)

end

I get response but :error_message returns Authentication failed...

Here is how it looks in SOAP UI

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ser="http://services.blahblah.com/service">
   <soap:Header>
      <ser:ICEAuthHeader>
         <!--Optional:-->
         <ser:Username>username</ser:Username>
         <!--Optional:-->
         <ser:Password>password</ser:Password>
      </ser:ICEAuthHeader>
   </soap:Header>
   <soap:Body>
      <ser:GetBrochure>
    <!--Optional:-->
         <ser:MappedID>123</ser:MappedID>
      </ser:GetBrochure>
   </soap:Body>
</soap:Envelope>

Upvotes: 2

Views: 4270

Answers (3)

Mukesh
Mukesh

Reputation: 931

client = Savon.client( wsdl: 'http://service.example.com?wsdl',soap_header: { "AuthenticateRequest" => {"apiKey" => "****** your api *********"}}, pretty_print_xml: true)

client.operations

response = client.call(:function)

here "apiKey" is my prams to authenticate api, your can be different

Upvotes: 0

adnann
adnann

Reputation: 197

Are the username and password sent with the SOAP request in the headers? If so, you could go about doing this when initializing the Savon client (see cannot set SOAP header parameters on savon call):

client = Savon.client(
    wsdl: SOAP_WSDL,
    endpoint: SOAP_URL,
    soap_header: { :username => username, :password => password }
)

Incidentally, if only certain calls need authentication, you can make calls like so:

response = client.call(
    :soap_call,
    :soap_header => { :username => username, :password => password },
    message: {message}
)

Upvotes: 1

Dani&#235;l Zwijnenburg
Dani&#235;l Zwijnenburg

Reputation: 2560

You could try this to authenticate using basic auth:

client = Savon::Client.new do
  wsdl.document = "http://services.blahblah.com/Service.asmx?WSDL"
  http.auth.basic "username", "password"
end

Upvotes: 0

Related Questions