使用Python Zeep调用FedEx追踪服务时遇405方法不允许错误
问题描述
尝试用Python的Zeep库集成FedEx追踪服务时,发送请求遇到「405 Client Error: Method Not Allowed」错误。已遵循FedEx开发指南操作,SoapUI中使用WSDL可成功运行,但Python用Zeep实现时出错。
出错代码片段
from zeep import Client from zeep.exceptions import Fault from zeep.transports import Transport from requests import Session from requests.auth import HTTPBasicAuth WSDL_URL = "https://wsbeta.fedex.com:443/web-services" # authentication for the WSDL URL session = Session() session.auth = HTTPBasicAuth("USERNAME', 'PASSWORD') transport_with_basic_auth = Transport(session=session) # Initialize the client with the FedEx WSDL URL client = Client(WSDL_URL, transport=transport_with_basic_auth) web_authentication_detail = { "UserCredential": { "Key": "---", "Password": "---" } } client_detail = { "AccountNumber": "---", "MeterNumber": "----" } tracking_number = "557----" # a request dictionary request_dict = { "WebAuthenticationDetail": web_authentication_detail, "ClientDetail": client_detail, "TransactionDetail": { "CustomerTransactionId": "Test Transaction" }, "Version": { "ServiceId": "trck", "Major": 20, "Intermediate": 0, "Minor": 0 }, "SelectionDetails": { "CarrierCode": None, "PackageIdentifier": { "Type": "TRACKING_NUMBER_OR_DOORTAG", "Value": tracking_number } } } try: # Send the tracking request response = client.service.track(**request_dict) print(response) except Fault as e: print("Error:", e)
错误信息
requests.exceptions.HTTPError: 405 Client Error: Method Not Allowed for url: https://wsbeta.fedex.com:443/web-services
已尝试操作
- 确认URL正确且可访问
- 在SoapUI中验证WSDL,可正常运行
- 检查凭证正确性
解决方案
问题原因
- WSDL路径错误:直接传入的是FedEx通用服务端点,而非追踪服务对应的具体WSDL文件URL,Zeep无法正确解析服务结构并发送请求。
- 多余的HTTP Basic Auth:FedEx SOAP服务的认证是通过请求体中的
WebAuthenticationDetail传递,额外的HTTP Basic Auth会干扰请求的正常发送。 - 请求方法不匹配:错误的URL端点不接受SOAP请求所需的POST方法,导致405错误。
修复步骤
- 替换为具体WSDL URL:使用FedEx测试环境追踪服务的专属WSDL地址:
https://wsbeta.fedex.com:443/web-services/TrackService_v20.wsdl - 移除HTTP Basic Auth代码:删除Session、Transport相关的基础认证逻辑,仅保留请求体中的凭证信息。
- 验证请求参数:确保
Version中的版本号与WSDL对应(v20版本的Major=20是正确的),SelectionDetails参数符合FedEx要求。
修正后的代码
from zeep import Client from zeep.exceptions import Fault # 使用追踪服务对应的具体WSDL URL WSDL_URL = "https://wsbeta.fedex.com:443/web-services/TrackService_v20.wsdl" # 直接初始化Client,无需额外HTTP认证 client = Client(WSDL_URL) web_authentication_detail = { "UserCredential": { "Key": "你的测试API Key", "Password": "你的测试API密码" } } client_detail = { "AccountNumber": "你的测试账号", "MeterNumber": "你的测试Meter号" } tracking_number = "557XXXXXX" request_dict = { "WebAuthenticationDetail": web_authentication_detail, "ClientDetail": client_detail, "TransactionDetail": { "CustomerTransactionId": "Test Transaction" }, "Version": { "ServiceId": "trck", "Major": 20, "Intermediate": 0, "Minor": 0 }, "SelectionDetails": { "CarrierCode": None, "PackageIdentifier": { "Type": "TRACKING_NUMBER_OR_DOORTAG", "Value": tracking_number } } } try: response = client.service.track(**request_dict) print(response) except Fault as e: print("Error:", e)
额外排查建议
- 确认所有测试凭证(Key、Password、AccountNumber、MeterNumber)均为FedEx测试环境提供的有效值。
- 若仍有问题,可启用Zeep调试日志,对比SoapUI的请求内容排查差异:
import logging logging.basicConfig(level=logging.DEBUG)
内容的提问来源于stack exchange,提问作者Mashail
相关产品推荐
相关产品推荐

