使用 Zeep 对 SOAP API 进行 Python 身份验证

时间:2021-02-24 17:04:10

标签: python api soap

我正在尝试使用 Zeep 库使用 SOAP API,但在调用方法时仍然收到相同的错误消息。我能够加载 wsdl 文件、创建参数对象等,但调用方法不起作用。 在提供者文档中提到“WebAPI 由消息级证书保护” 我使用 openssl (openssl.exe pkcs12 -in certificate.pfx -out Certificate.pem -nodes) 将 .pfx 证书(也安装在我连接到 API 的服务器上)转换为 .pem

from requests import Session
from zeep.wsse.username import UsernameToken
from zeep import Client, Settings
from zeep.transports import Transport

wsdl = r'http://[LINK]/WebService/WebAPI?singleWsdl'

pem_cert = r'E:\[PATH]\certificate.pem'

session = Session()
session.cert = pem_cert

username_token = UsernameToken('Username', 'Password')

client = Client(wsdl,wsse=username_token, transport=Transport(session=session))
client_bind = client.bind('WebAPI','CorrectBinding')

factory = client.type_factory('ns2')
param = factory.WebAPI_Parameters()
param.Items = 'Item1, Item2, item3'

response = client_bind.GetData(param)

这给了我错误:

<块引用>

zeep.exceptions.Fault:无法处理消息。这是 很可能是因为这个动作 'http://[LINK]/WebService/WebAPI/GetData' 是 不正确或因为消息包含无效或过期 安全上下文令牌或因为之间存在不匹配 绑定。如果服务,安全上下文令牌将无效 由于不活动而中止通道。为了防止服务 过早中止空闲会话会增加接收超时 服务端点的绑定。

我不确定我应该再尝试什么,我尝试使用来自 requests.auth 的 HTTPBasicAuth 和来自 https://docs.python-zeep.org/en/master/wsse.html 的“UsernameToken and Signature together”进行身份验证,但结果是一样的。

有人知道我做错了什么吗?

这是一个配置文件

<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="CorrectBinding">
                    <security>
                        <message clientCredentialType="UserName" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://[LINK]/WebService/WebAPI"
                binding="wsHttpBinding" bindingConfiguration="CorrectBinding"
                contract="WebAPI" name="CorrectBinding">
                <identity>
                    <certificate encodedValue="[thumbprint of certificate]" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

PS:如果我尝试使用 SUDS 进行类似操作,则会收到“异常:(400,'错误请求')”

0 个答案:

没有答案