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

2024-09-30 06:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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: The message could not be processed. This is most likely because the action 'http://[LINK]/WebService/WebAPI/GetData' is incorrect or because the message contains an invalid or expired security context token or because there is a mismatch between bindings. The security context token would be invalid if the service aborted the channel due to inactivity. To prevent the service from aborting idle sessions prematurely increase the Receive timeout on the service endpoint's binding.

我不确定我应该尝试什么,我尝试了从requests.auth和https://docs.python-zeep.org/en/master/wsse.html“UsernameToken和Signature Token一起”使用HTTPBasicAuth进行身份验证,但结果是相同的

有人知道我做错了什么吗

这是一个配置文件

<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:如果我尝试使用类似的肥皂水,那么我会收到“异常:(400,'错误请求')”


Tags: thefromimportclienttokensession错误service

热门问题