我是第一次调用基于 SOAP 的 API,我从文档中获得了以下信息:
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==
我正在使用 Savon gem 打电话。我如何使用 Ruby 1.9.3 和 Savon gem 验证 API 调用 api_call
给定上述信息?
这就是我用来设置 WSDL 或 client
的内容。
client = Savon.client(wsdl: "https://api.five9.com/wsadmin/v2/AdminWebService?wsdl&user=luigi@apitest.com")
最佳答案
您需要在消息的 SOAP header 中包含身份验证凭据。 Savon 为其提供了 :soap_header 符号。
您的示例可能如下所示:
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
)
我无法在这个示例中对其进行测试,因为我缺少用户凭据,但它通过了“ruby -c”。
关于ruby - 如何使用 Ruby Savon 编写 SOAP 身份验证 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19284392/