我正在尝试使用 DHL 的 API 添加货件详细信息并获得标签作为回报。我正在使用带有 SoapUI 的肥皂盒并能够发出请求。现在我想在 Python 中做到这一点。我不太确定这些步骤。我偶然发现了这个:DHL Soap Request Python
有人可以帮我让它运行吗?身份验证正在工作,但我不知道如何构建肥皂请求。
第一次尝试是使用 SoapUI 软件来做一些简单的请求或测试一切。凭借我的凭据,我能够发送请求并获得标签作为回报。
作为端点,使用了以下 URL,取自文档:https ://cig.dhl.de/services/sandbox/soap
XML 如下所示:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cis="http://dhl.de/webservice/cisbase" xmlns:ns="http://dhl.de/webservices/businesscustomershipping/3.0">
<soapenv:Header>
<cis:Authentification>
<cis:user>2222222222_01</cis:user>
<cis:signature>pass</cis:signature>
</cis:Authentification>
</soapenv:Header>
<soapenv:Body>
<ns:CreateShipmentOrderRequest>
<ns:Version>
<majorRelease>3</majorRelease>
<minorRelease>1</minorRelease>
</ns:Version>
<ShipmentOrder>
<sequenceNumber></sequenceNumber>
<Shipment>
<ShipmentDetails>
<product>V62WP</product>
<cis:accountNumber>${#Project#testAccountNumberV62WP}</cis:accountNumber>
<customerReference>123456</customerReference>
<shipmentDate>2021-09-03</shipmentDate>
<costCentre></costCentre>
<ShipmentItem>
<weightInKG>1</weightInKG>
<lengthInCM>25</lengthInCM>
<widthInCM>15</widthInCM>
<heightInCM>1</heightInCM>
</ShipmentItem>
<Service>
</Service>
<Notification>
<recipientEmailAddress>empfaenger@test.de</recipientEmailAddress>
</Notification>
</ShipmentDetails>
<Shipper>
<Name>
<cis:name1>Absender Zeile 1</cis:name1>
<cis:name2>Absender Zeile 2</cis:name2>
<cis:name3>Absender Zeile 3</cis:name3>
</Name>
<Address>
<cis:streetName>Vegesacker Heerstr.</cis:streetName>
<cis:streetNumber>111</cis:streetNumber>
<cis:zip>28757</cis:zip>
<cis:city>Bremen</cis:city>
<cis:Origin>
<cis:country></cis:country>
<cis:countryISOCode>DE</cis:countryISOCode>
</cis:Origin>
</Address>
<Communication>
<!--Optional:-->
<cis:phone>+49421987654321</cis:phone>
<cis:email>absender@test.de</cis:email>
<!--Optional:-->
<cis:contactPerson>Kontaktperson Absender</cis:contactPerson>
</Communication>
</Shipper>
<Receiver>
<cis:name1>Name</cis:name1>
<Address>
<cis:name2>Empfänger Zeile 2</cis:name2>
<cis:name3>Empfänger Zeile 3</cis:name3>
<cis:streetName>Street</cis:streetName>
<cis:streetNumber>Number</cis:streetNumber>
<cis:zip>zipCode</cis:zip>
<cis:city>City</cis:city>
<cis:Origin>
<cis:country></cis:country>
<cis:countryISOCode>DE</cis:countryISOCode>
</cis:Origin>
</Address>
<Communication>
<cis:phone>+49421123456789</cis:phone>
<cis:email>empfaenger@test.de</cis:email>
<cis:contactPerson>Kontaktperson Empfänger</cis:contactPerson>
</Communication>
</Receiver>
</Shipment>
<PrintOnlyIfCodeable active="1"/>
</ShipmentOrder>
<labelResponseType>URL</labelResponseType>
<groupProfileName></groupProfileName>
<labelFormat></labelFormat>
<labelFormatRetoure></labelFormatRetoure>
<combinedPrinting>0</combinedPrinting>
</ns:CreateShipmentOrderRequest>
</soapenv:Body>
</soapenv:Envelope>
我的 Python 代码如下所示:
from requests import Session
from requests.auth import HTTPBasicAuth
from zeep import Client, xsd
from zeep.transports import Transport
user = "my_user_name"
password = "my_password"
USER = "2222222222_01" #from doc
PASSWORD = "pass" #from doc
EKP = "2222222222"
wsdl = "./geschaeftskundenversand-api-3.1.8.wsdl" #downloaded and stored local
session = Session()
# Authenticate with gateway
session.auth = HTTPBasicAuth(user, password)
client = Client(wsdl, transport=Transport(session=session))
# Build Authentification header for API-Endpoint using zeep xsd
header = xsd.Element(
'{http://test.python-zeep.org}Authentification',
xsd.ComplexType([
xsd.Element(
'{http://test.python-zeep.org}user',
xsd.String()),
xsd.Element(
'{http://test.python-zeep.org}signature',
xsd.String()),
])
)
header_value = header(user = USER, signature = PASSWORD)
client.service.createShipmentOrder(_soapheaders=[header_value])
结果,我得到了回溯:缺少元素版本
在我看来,身份验证正在工作,现在我需要插入 XML 的正文部分。我的理解是,下一步应该是:
client.service.createShipmentOrder
例如,在这里我可以传递一本字典。createShipmentOrder 是纪录片中所述的请求之一。它返回一个状态正常的 XML、一个货运编号和一个用于打印的标签的 URL。在我看来,现在我必须传递 XML 中所述的地址等。在 SoupUI 中,请求被发送到端点 URL https://cig.dhl.de/services/sandbox/soap。但是在我的 Python 代码中没有说明端点。我需要它还是它是我本地存储的 wsdl 文件的一部分?