How to write SOAP Authentication header with Ruby Savon

六月ゝ 毕业季﹏ 提交于 2019-12-21 20:30:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!