问题
I am making calls to a SOAP based API for the first time, and I have the following info from the documentation:
In your client, construct your authorization header as follows:
1 Concatenate the user name and password, for example: ExampleUsername:ExamplePassword
2 Encode the string in base 64, for example: RXhhbXBsZVVzZXJOYW1lOkV4YW1wbGVQYXNzd29yZA==
3 In your code, enter the Authorization header with the value Basic .
Example Web Services header with encoded user name and password
POST https://api.five9.com/wsadmin/AdminWebService HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
Authorization: Basic RXhhbXBsZVVzZXJOYW1lOkV4YW1wbGVQYXNzd29yZA==
I am using the Savon gem to make my call. How would I, using Ruby 1.9.3 and the Savon gem, authenticate an API call api_call
given the above information?
This is what I am using to setup the WSDL, or client
.
client = Savon.client(wsdl: "https://api.five9.com/wsadmin/v2/AdminWebService?wsdl&user=luigi@apitest.com")
回答1:
You need to include the authentification credentials in the SOAP header of your message. Savon offers the :soap_header symbol for it.
Your example could look like this:
require 'savon'
require 'securerandom'
realm = Base64.strict_encode64("ExampleUsername:ExamplePassword")
client = Savon.client(
wsdl: "https://api.five9.com/wsadmin/v2/AdminWebService?wsdl",
soap_header: { 'Authorization:' => "Basic #{realm}"},
log: true,
log_level: debug,
pretty_print_xml: true
)
I couldn't test it this example because I lack the user credentials, but it passed "ruby -c".
来源:https://stackoverflow.com/questions/19284392/how-to-write-soap-authentication-header-with-ruby-savon