Monday, March 27, 2006

SOAP + Basic Authentication in Ruby

Do you need to do SOAP in Ruby?

First of all, install the latest version of Ruby you can stand. (I'm using this one. What the heck. I'm giving the laptop back in a few hours.)

That's a fine thing, but you can't do Basic Authentication yet. You need to install a net/http that understands Basic Authentication.

Ready to go? Not quite. The version of SOAP4R you have doesn't handle arguments to web services correctly. You need to install the latest SOAP4R to get your arguments handled right.

Now you're ready. You might get an extraneous error message "ignored attr: {}mixed" but it probably won't affect your ability to do the job.

Many many thanks to Emil Marceta for walking me through the process.

Two things:
1) Perl's SOAP::Lite does this out of the box.
2) The Pickaxe dismisses SOAP pretty perfunctorily, but the beauty of SOAP is that it is (should be) very very easy for the client, using existing libraries.

What would it take to put this in the regular Windows Ruby distro?

7 comments:

tomeeo said...

Chris, thanks for the help, but even with the soap4r update & the net/http-access2 udpate, i still am getting an HTTP 401 unauthorized when i try to call my web service method. I am trying to accessing a set of sharepoint web services. The site askes for a username and password when I visit it and that is what I have been supplying:

ws.options["protocol.http.basic_auth"] << ['site_url', "username", "password"]

I've also tried the headerhandler approach but no dice.

Any debugging tips?

Chris McMahon said...

There are only two ways to get a 401 error in this context: either you have the wrong user/password combination; or you are requesting a page that is protected. I suspect that the 'site_url' is somehow incorrect. What's there when you navigate a browser to 'site_url'? I think in general there should be a WSDL there.

Alex said...

Hi

I been trying to access the web services inside xplanner with no luck the following is my code:
class WelcomeController < ApplicationController

require 'soap/wsdlDriver'
require 'soap/driver'
require 'cgi'
require 'net/http'

def index

soap_client = SOAP::WSDLDriverFactory.new("http://10.20.30.158:8080/xplanner/soap/XPlanner?wsdl")
soap_client.options["protocol.http.basic_auth"] << ['http://10.20.30.158:8080/xplanner/soap/XPlanner?wsdl', "sysadmin", "password"]
driver = soap_client.createDriver

# driver = SOAP::WSDLDriverFactory.new('http:10.20.30.158:8080:8080/xplanner/soap/XPlanner?wsdl').create_rpc_driver
# driver.options["protocol.http.basic_auth"] << ['http://10.20.30.158:8080:8080/xplanner', "sysadmin", "password"]

# wsdl_url = 'http://10.20.30.158:8080/xplanner/soap/XPlanner?wsdl'
# driver = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver

puts driver.inspect

end
eni get a 401 error

and yes user and password are the correct username and password

Chris McMahon said...

alex, some of your code looks odd

Under the "# driver" part, the url is malformed, missing slash characters.

I think the comment editor is going to truncate the create_rpc_driver line, but try the code below and see if you get a 401

wsdl_url = "http://foo/wsdl"
user = "username"
pass = "password

@soap = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver
@soap.options["protocol.http.basic_auth"] << [wsdl_url,user,pass]

Alex said...

Hi thanks for your replay.

Yes you are right the code was a bit odd, sorry it was late.

I have tried the code that you gave me and I get 2 errors, One when i run a ruby script which is the 401 error:
retry_connect: Unexpected response: #HTTP:: blah blah blah ,@response_status_code=401 etc ...

The other one when I call the webs ervice from rails:

uninitialized constant WSDLDriverFactory

Again Thanks for your help

Michael McGrath said...

Can anyone help me with calling over SSL?


def lookup_id

pid=params[:id]

driver = SOAP::WSDLDriverFactory.new("https://blahblah.wsdl").create_rpc_driver

@rv=driver.verifyId(AUTH_TOKEN, PIN, pid)


This works fine locally but when deployed I get:

OpenSSL::SSL::SSLError (certificate verify failed):

Can anyone enlighten me as to how I can simply ignore the cert validity error as happens on my local PC?

Sandopi said...

Hello,
I have to use authentification by the HTTP header but i can not add a "Authorization" parameter to the HTTPHeader. I get the services's descriptions by WSDL using SOAP::WSDLDriverFactory.new(...).create_rpc_driver.

This is an old post but is there anyone to help me please?