我正在尝试使用DHL的API来添加装运详细信息,并得到一个标签作为回报。我将soapbox与SoapUI一起使用,并且能够发出请求。现在我想用Python来做这件事。我不太确定这些步骤。我偶然发现了这个:DHL Soap Request Python
有人能帮我把这个打开吗?身份验证正在工作,但我不知道如何构建soap请求
第一次尝试是使用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
这里我可以传递一本字典。 CreateShipmenter是文档中所述的请求之一。它返回一个状态为OK的XML、装运编号和标签的URL以供打印。 在我看来,现在我必须传递XML中所述的地址等等。 在SoupUI中,请求被发送到端点URLhttps://cig.dhl.de/services/sandbox/soap。但是在我的Python代码中没有说明端点。我需要它吗,还是它是我存储在本地的wsdl文件的一部分
因此,由于没有人能帮助我,我自己做了:)
我需要几个小时来理解API,但现在很清楚
您可以使用我问题中的大部分代码。 您必须生成包含所有所需数据的嵌套字典。例如:
将wsdl文件保存到文件夹中,并将脚本链接到该文件夹,如下所示:
您需要在网关上为请求和身份验证提供标头。验证通过一个简单的请求运行,如问题中所示
您现在只需要soap请求:
如果你运行所有程序,你会得到一个回复,上面写着“OK”,里面有发货编号和标签的url
就这样
我在Raspberry Pi4上运行一些Streamlight部分的脚本,让标签打印机打印所有内容。以下是打印标签的代码:
相关问题 更多 >
编程相关推荐