【发布时间】:2022-02-06 22:29:00
【问题描述】:
我正在调用返回 XML 的 SOAP API 并收到以下错误
错误 -
title>401 - Unauthorized: Access is denied due to invalid credentials.</title>
我已经使用了我可以在其他 SO 查询、SOAP 信息和我的特定调用文档中找到的信息,这是我迄今为止为它整理的代码
String soap =
'''<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://thalesgroup.com/RTTI/2014-02-20/ldb/"
xmlns:ns2="http://thalesgroup.com/RTTI/2017-10-01/ldb/GetDepartureBoard">
<SOAP-ENV:Header>
<ns2:AccessToken>
<ns2:TokenValue>my_token</ns2:TokenValue>
</ns2:AccessToken>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetDepartureBoardRequest>
<ns1:numRows>10</ns1:numRows>
<ns1:crs>MAN</ns1:crs>
</ns1:GetDepartureBoardRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>''';
// Send the POST request, with full SOAP envelope as the request body.
http.Response response = await http.post(Uri.parse(
'https://lite.realtime.nationalrail.co.uk/OpenLDBWS/ldb11.asmx'),
headers: {
'Content-Type': 'text/xml; charset=utf-8',
'SOAPAction': 'http://thalesgroup.com/RTTI/2017-10-01/ldb/GetDepartureBoard'
},
body: soap
);
var rawXmlResponse = response.body;
final parsedXml = XmlDocument.parse (rawXmlResponse);
print(parsedXml);
}
API 的提供者在他们的文档中声明
This token shall be passed as a SOAP Header value.
我包含在body 中是否足够或我如何在该部分中引用它...
headers: {
'Content-Type': 'text/xml; charset=utf-8',
'SOAPAction': 'http://thalesgroup.com/RTTI/2017-10-01/ldb/GetDepartureBoard'
这是一个例子 - https://wiki.openraildata.com/index.php/GetDepBoardWithDetails
数据来自 - https://realtime.nationalrail.co.uk/OpenLDBWS/
谢谢
更新调用
String soap =
'''
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://thalesgroup.com/RTTI/2017-10-01/ldb/"
xmlns:ns2="http://thalesgroup.com/RTTI/2013-11-28/Token/ns2es">
<SOAP-ENV:Header>
<ns2:AccessToken>
<ns2:TokenValue>my_token</ns2:TokenValue>
</ns2:AccessToken>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetDepartureBoardRequest>
<ns1:numRows>10</ns1:numRows>
<ns1:crs>MAN</ns1:crs>
</ns1:GetDepartureBoardRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>''';
// Send the POST request, with full SOAP envelope as the request body.
http.Response response = await http.post(Uri.parse(
'https://realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2017-10-01'),
headers: {
'SOAPAction': 'http://thalesgroup.com/RTTI/2017-10-01/ldb/GetDepartureBoard'
},
body: soap
);
更新回复
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://thalesgroup.com/RTTI/2017-10-01/ldb/" targetNamespace="http://thalesgroup.com/RTTI/2017-10-01/ldb/">
<wsdl:import namespace="http://thalesgroup.com/RTTI/2017-10-01/ldb/" location="rtti_2017-10-01_ldb.wsdl"/>
<wsdl:service name="ldb">
<wsdl:port name="LDBServiceSoap" binding="tns:LDBServiceSoap">
<soap:address location="https://realtime.nationalrail.co.uk/OpenLDBWS/ldb11.asmx"/>
</wsdl:port>
<wsdl:port name="LDBServiceSoap12" binding="tns:LDBServiceSoap12">
<soap12:address location="https://realtime.nationalrail.co.uk/OpenLDBWS/ldb11.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
这是哪个网页 - https://realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2017-10-01
【问题讨论】:
-
您的
ns2看起来完全错误。将其与链接的示例进行比较,您会发现它应该是"http://thalesgroup.com/RTTI/2013-11-28/Token/types"您是否尝试过示例中的确切 XML(但使用您的访问令牌)?另一件小事:你已经说过你的内容在内容标题中是 utf-8,所以让正文utf8.encode(soap)否则http将为你编码它在 Latin-1 中,大部分时间都是一样的,直到它不是...