如何使用 Ruby Savon 编写 SOAP 身份验证标头 [英] How to write SOAP Authentication header with Ruby Savon

查看:33
本文介绍了如何使用 Ruby Savon 编写 SOAP 身份验证标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次调用基于 SOAP 的 API,我从文档中获得以下信息:

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 连接用户名和密码,例如:示例用户名:示例密码

1 Concatenate the user name and password, for example: ExampleUsername:ExamplePassword

2 以 base 64 编码字符串,例如:RXhhbXBsZVVzZXJOYW1lOkV4YW1wbGVQYXNzd29yZA==

2 Encode the string in base 64, for example: RXhhbXBsZVVzZXJOYW1lOkV4YW1wbGVQYXNzd29yZA==

3 在您的代码中,输入值为 Basic 的 Authorization 标头.

3 In your code, enter the Authorization header with the value Basic .

带有编码的用户名和密码的 Web 服务标头示例

Example Web Services header with encoded user name and password

POST https://api.five9.com/wsadmin/AdminWebService HTTP/1.1

POST https://api.five9.com/wsadmin/AdminWebService HTTP/1.1

接受编码:gzip,deflate

Accept-Encoding: gzip,deflate

内容类型:text/xml;charset=UTF-8

Content-Type: text/xml;charset=UTF-8

SOAPAction:""

SOAPAction: ""

授权:基本RXhhbXBsZVVzZXJOYW1lOkV4YW1wbGVQYXNzd29yZA==

Authorization: Basic RXhhbXBsZVVzZXJOYW1lOkV4YW1wbGVQYXNzd29yZA==

我正在使用 Savon gem 拨打电话.鉴于上述信息,我将如何使用 Ruby 1.9.3 和 Savon gem 验证 API 调用 api_call?

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?

这是我用来设置 WSDL 或 client 的内容.

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")

推荐答案

您需要在消息的 SOAP 标头中包含身份验证凭据.Savon 为其提供了 :soap_header 符号.

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
)

我无法在此示例中对其进行测试,因为我缺少用户凭据,但它通过了ruby -c".

I couldn't test it this example because I lack the user credentials, but it passed "ruby -c".

这篇关于如何使用 Ruby Savon 编写 SOAP 身份验证标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆