Query accounts using Autotask API in Java

oazcurra :

I’ve bee trying to use the Autotask API from Java without success. I’m building the SOAP message like this:

SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
soapEnvelope.addNamespaceDeclaration("atns", AUTOTASK_NAMESPACE);
MimeHeaders soapHeaders = soapMessage.getMimeHeaders();
String authorization = Base64Utils.encodeBasicAuthorization(username, password);
soapHeaders.addHeader("Authorization", "Basic " + authorization);
if (!StringUtils.isBlank(integrationCode)) {
    SOAPElement autotaskIntegrationsElement = soapMessage.getSOAPHeader().addChildElement("AutotaskIntegrations", null, AUTOTASK_NAMESPACE);
    SOAPElement integrationCodeElement = autotaskIntegrationsElement.addChildElement("IntegrationCode");
    integrationCodeElement.setTextContent(integrationCode);
}
SOAPBody soapBody = soapMessage.getSOAPBody();
SOAPElement methodElement = soapBody.addChildElement("query", "atns");
CDATASection query = soapMessage.getSOAPPart().createCDATASection(queryXml));
methodElement.appendChild(query);
SOAPMessage soapResponse = soapConnection.call(soapMessage, defaultZoneUrl);

This is returning a ATWSError. What I’m doing wrong?

dgaviola :

You are missing the sXML element:

SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
soapEnvelope.addNamespaceDeclaration("atns", AUTOTASK_NAMESPACE);
MimeHeaders soapHeaders = soapMessage.getMimeHeaders();
String authorization = Base64Utils.encodeBasicAuthorization(username, password);
soapHeaders.addHeader("Authorization", "Basic " + authorization);
if (!StringUtils.isBlank(integrationCode)) {
    SOAPElement autotaskIntegrationsElement = soapMessage.getSOAPHeader().addChildElement("AutotaskIntegrations", null, AUTOTASK_NAMESPACE);
    SOAPElement integrationCodeElement = autotaskIntegrationsElement.addChildElement("IntegrationCode");
    integrationCodeElement.setTextContent(integrationCode);
}
SOAPBody soapBody = soapMessage.getSOAPBody();
SOAPElement methodElement = soapBody.addChildElement("query", "atns");
SOAPElement sxmlElement = methodElement.addChildElement("sXML", "atns");
CDATASection query = soapMessage.getSOAPPart().createCDATASection(queryXml));
sxmlElement.appendChild(query);
SOAPMessage soapResponse = soapConnection.call(soapMessage, defaultZoneUrl);

That should work.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326324&siteId=1