我如何使用业务中心 (Java) 的 Web 服务? (解决了)
How can I consume web service from business central (Java)? (SOLVED)
我想使用 Business Central Web 服务,以便将其添加到现有的 Web 应用程序。
我试过:
- 使用授权对 Postman 进行测试 header(有效)
- 测试 SoapUI(有效)
但是我收到这个错误:
com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0008: Bad Response; Unauthorized
Exception in thread "main" com.sun.xml.messaging.saaj.SOAPExceptionImpl: java.security.PrivilegedActionException: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Bad response: (401Unauthorized
at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:171)
代码:
String webPage = "https://api.businesscentral.dynamics.com/v2.0/<tenant>/...";
String name = "username";
String password = "password";
String authString = name + ":" + password;
String authEncBytes = new String(Base64.getEncoder().encode(authString.getBytes(StandardCharsets.UTF_8)));
String authStringEnc = new String(authEncBytes);
URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.getHeader().detachNode();
envelope.addHeader();
MimeHeaders hd = soapMessage.getMimeHeaders();
hd.addHeader("Authorization", "Basic " + authEncBytes);
SOAPBody soapBodytxaber = envelope.getBody();
envelope.addNamespaceDeclaration("myNamespace", "uri");
SOAPElement soapBodyElem0txa = soapBodytxaber.addChildElement("Read", "myNamespace");
SOAPElement soapBodyElem1txa = soapBodyElem0txa.addChildElement("No", "myNamespace");
soapBodyElem1txa.addTextNode("00000058");
SOAPConnectionFactory soapConnectionFactory = OAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
soapMessage.saveChanges();
SOAPMessage soapResponse = soapConnection.call(soapMessage, url);
您需要确保您尝试验证的用户具有 Web 服务访问密钥。
那么您必须在验证时使用 Web 服务访问密钥作为密码。
在这行代码中,Basic
之后似乎还缺少 space
:
hd.addHeader("Authorization", "Basic" + authEncBytes);
应该是
hd.addHeader("Authorization", "Basic " + authEncBytes);
解决方案
好吧,在收到课程后,他们告诉我基本授权已被弃用,他们现在使用 OAuth 2...
感谢回复
问候
我想使用 Business Central Web 服务,以便将其添加到现有的 Web 应用程序。
我试过:
- 使用授权对 Postman 进行测试 header(有效)
- 测试 SoapUI(有效)
但是我收到这个错误:
com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0008: Bad Response; Unauthorized
Exception in thread "main" com.sun.xml.messaging.saaj.SOAPExceptionImpl: java.security.PrivilegedActionException: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Bad response: (401Unauthorized
at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:171)
代码:
String webPage = "https://api.businesscentral.dynamics.com/v2.0/<tenant>/...";
String name = "username";
String password = "password";
String authString = name + ":" + password;
String authEncBytes = new String(Base64.getEncoder().encode(authString.getBytes(StandardCharsets.UTF_8)));
String authStringEnc = new String(authEncBytes);
URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.getHeader().detachNode();
envelope.addHeader();
MimeHeaders hd = soapMessage.getMimeHeaders();
hd.addHeader("Authorization", "Basic " + authEncBytes);
SOAPBody soapBodytxaber = envelope.getBody();
envelope.addNamespaceDeclaration("myNamespace", "uri");
SOAPElement soapBodyElem0txa = soapBodytxaber.addChildElement("Read", "myNamespace");
SOAPElement soapBodyElem1txa = soapBodyElem0txa.addChildElement("No", "myNamespace");
soapBodyElem1txa.addTextNode("00000058");
SOAPConnectionFactory soapConnectionFactory = OAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
soapMessage.saveChanges();
SOAPMessage soapResponse = soapConnection.call(soapMessage, url);
您需要确保您尝试验证的用户具有 Web 服务访问密钥。
那么您必须在验证时使用 Web 服务访问密钥作为密码。
在这行代码中,Basic
之后似乎还缺少 space
:
hd.addHeader("Authorization", "Basic" + authEncBytes);
应该是
hd.addHeader("Authorization", "Basic " + authEncBytes);
解决方案
好吧,在收到课程后,他们告诉我基本授权已被弃用,他们现在使用 OAuth 2...
感谢回复
问候