web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM dynamics authentication connection using soap with java from local IP

(1) ShareShare
ReportReport
Posted on by

I tried to authenticate the crm dynamics using soap via local ip for past 10 days. tried different options. 

Getting error like 'to unblock the Cooke'. 

And i changed my browser setting as allow cookes and added url to trusted sites. 

But still getting same issue. 

Can you please some help me to resolve the issue??

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Check following - github.com/.../CRMSoapAuthJava

  • Community Member Profile Picture
    on at

    Thanks for the reply Andrew.

    But what is the reason behind getting response as  html format. massage contains 'please

    change setting as unblock the Cooke in browser setting'

    It is getting even changed my settings also.

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Mahes,

    Unfortunately (or luckily) I have no superpowers to know what's wrong with your code and I would not be able to help you until you show it.

  • Community Member Profile Picture
    on at

    Now we are getting below error Andrew.

    org.xml.sax.SAXParseExceptionpublicId: -//W3C//DTD HTML 4.01//EN; systemId: www.w3.org/.../strict.dtd; lineNumber: 81; columnNumber: 5; The declaration for the entity "ContentType" must end with '>'.

    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)

    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)

    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)

    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)

    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1472)

    at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanEntityDecl(XMLDTDScannerImpl.java:1604)

    at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDecls(XMLDTDScannerImpl.java:2028)

    at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDTDExternalSubset(XMLDTDScannerImpl.java:306)

    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1174)

    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1045)

    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:959)

  • Community Member Profile Picture
    on at

    I am using below code Andrew.

    class PushEnquiryFeedsToClientCrm {

     String username = "username";

      String password = "password";

      String endPointurl = "https://insightdev.crm6.dynamics.com";

      @RequestMapping(value = "path", method = { RequestMethod.GET, RequestMethod.POST })

      public String pushEnquiryFeedsToClientCrm(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception {

    CrmAuth auth = new CrmAuth();

    CrmAuthenticationHeader authHeader = auth.GetHeaderOnline(username, password, endPointurl);

    return null;

      }

    }  

    public class CrmAuth {

      public CrmAuthenticationHeader GetHeaderOnline(String username, String password, String url) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException, Exception {

    if (!url.endsWith("/"))

      url += "/";

    String urnAddress = "insightdev.crm6.dynamics.com/";

    Date now = new Date();

    StringBuilder xml = new StringBuilder();

    xml.append("<s:Envelope xmlns:s=\"www.w3.org/.../soap-envelope\" xmlns:a=\"www.w3.org/.../addressing\" xmlns:u=\"docs.oasis-open.org/.../oasis-200401-wss-wssecurity-utility-1.0.xsd\">");

    xml.append("<s:Header>");

    xml.append("<a:Action s:mustUnderstand=\"1\">schemas.xmlsoap.org/.../Issue&lt;/a:Action>");

    xml.append("<a:MessageID>urn:uuid:" + java.util.UUID.randomUUID() + "</a:MessageID>");

    xml.append("<a:ReplyTo>");

    xml.append("<a:Address>www.w3.org/.../anonymous&lt;/a:Address>");

    xml.append("</a:ReplyTo>");

    xml.append("<a:To s:mustUnderstand=\"1\">login.microsoftonline.com/RST2.srf&lt;/a:To>");

    xml.append("<o:Security s:mustUnderstand=\"1\" xmlns:o=\"docs.oasis-open.org/.../oasis-200401-wss-wssecurity-secext-1.0.xsd\">");

    xml.append("<u:Timestamp u:Id=\"_0\">");

    xml.append("<u:Created>" + String.format("%tFT%<tT.%<tLZ", now) + "</u:Created>");

    xml.append("<u:Expires>" + String.format("%tFT%<tT.%<tLZ", AddMinutes(60, now)) + "</u:Expires>");

    xml.append("</u:Timestamp>");

    xml.append("<o:UsernameToken u:Id=\"uuid-" + java.util.UUID.randomUUID() + "-1\">");

    xml.append("<o:Username>" + username + "</o:Username>");

    xml.append("<o:Password>" + password + "</o:Password>");

    xml.append("</o:UsernameToken>");

    xml.append("</o:Security>");

    xml.append("</s:Header>");

    xml.append("<s:Body>");

    xml.append("<trust:RequestSecurityToken xmlns:trust=\"schemas.xmlsoap.org/.../trust\">");

    xml.append("<wsp:AppliesTo xmlns:wsp=\"schemas.xmlsoap.org/.../policy\">");

    xml.append("<a:EndpointReference>");

    xml.append("<a:Address>urn:" + urnAddress + "</a:Address>");

    xml.append("</a:EndpointReference>");

    xml.append("</wsp:AppliesTo>");

    xml.append("<trust:RequestType>schemas.xmlsoap.org/.../Issue&lt;/trust:RequestType>");

    xml.append("</trust:RequestSecurityToken>");

    xml.append("</s:Body>");

    xml.append("</s:Envelope>");

    URL LoginURL = new URL(url);

    HttpURLConnection rc = (HttpURLConnection) LoginURL.openConnection();

    rc.setRequestMethod("POST");

    rc.setDoOutput(true);

    rc.setDoInput(true);

    rc.setRequestProperty("Content-Type", "application/xml; charset=UTF-8");

    String reqStr = xml.toString();

    int len = reqStr.length();

    rc.setRequestProperty("Content-Length", Integer.toString(len));

    rc.connect();

    OutputStreamWriter out = new OutputStreamWriter(rc.getOutputStream());

    out.write(reqStr, 0, len);

    out.flush();

    InputStreamReader read = new InputStreamReader(rc.getInputStream());

    StringBuilder sb = new StringBuilder();

    int ch = read.read();

    while (ch != -1) {

      sb.append((char) ch);

      ch = read.read();

    }

    String response = sb.toString();

    read.close();

    rc.disconnect();

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder = builderFactory.newDocumentBuilder();

    Document x = builder.parse(new ByteArrayInputStream(response.getBytes()));

    NodeList cipherElements = x.getElementsByTagName("CipherValue");

    String token1 = cipherElements.item(0).getTextContent();

    String token2 = cipherElements.item(1).getTextContent();

    NodeList keyIdentiferElements = x.getElementsByTagName("wsse:KeyIdentifier");

    String keyIdentifer = keyIdentiferElements.item(0).getTextContent();

    NodeList tokenExpiresElements = x.getElementsByTagName("wsu:Expires");

    String tokenExpires = tokenExpiresElements.item(0).getTextContent();

    Calendar c = DatatypeConverter.parseDateTime(tokenExpires);

    CrmAuthenticationHeader authHeader = new CrmAuthenticationHeader();

    authHeader.Expires = c.getTime();

    authHeader.Header = CreateSoapHeaderOnline(url, keyIdentifer, token1, token2);

    return authHeader;

      }

      /**

       * @return Date The date with added minutes.

       * @param minutes

       * Number of minutes to add.

       * @param time

       * Date to add minutes to.

       */

      private Date AddMinutes(int minutes, Date time) {

    long ONE_MINUTE_IN_MILLIS = 60000;

    long currentTime = time.getTime();

    Date newDate = new Date(currentTime + (minutes * ONE_MINUTE_IN_MILLIS));

    return newDate;

      }

      /**

       * Gets a CRM Online SOAP header.

       *

       * @return String The XML SOAP header to be used in future requests.

       * @param url

       * The Url of the CRM Online organization

       * (https://org.crm.dynamics.com).

       * @param keyIdentifer

       * The KeyIdentifier from the initial request.

       * @param token1

       * The first token from the initial request.

       * @param token2

       * The second token from the initial request..

       */

      public String CreateSoapHeaderOnline(String url, String keyIdentifer, String token1, String token2) {

    StringBuilder xml = new StringBuilder();

    xml.append("<s:Header>");

    xml.append("<a:Action s:mustUnderstand=\"1\">schemas.microsoft.com/.../Execute&lt;/a:Action>");

    xml.append("<Security xmlns=\"docs.oasis-open.org/.../oasis-200401-wss-wssecurity-secext-1.0.xsd\">");

    xml.append("<EncryptedData Id=\"Assertion0\" Type=\"www.w3.org/.../xmlenc\" xmlns=\"www.w3.org/.../xmlenc\">");

    xml.append("<EncryptionMethod Algorithm=\"www.w3.org/.../xmlenc\"/>");

    xml.append("<ds:KeyInfo xmlns:ds=\"www.w3.org/.../xmldsig\">");

    xml.append("<EncryptedKey>");

    xml.append("<EncryptionMethod Algorithm=\"www.w3.org/.../xmlenc\"/>");

    xml.append("<ds:KeyInfo Id=\"keyinfo\">");

    xml.append("<wsse:SecurityTokenReference xmlns:wsse=\"docs.oasis-open.org/.../oasis-200401-wss-wssecurity-secext-1.0.xsd\">");

    xml.append("<wsse:KeyIdentifier EncodingType=\"docs.oasis-open.org/.../oasis-200401-wss-soap-message-security-1.0\" ValueType=\"docs.oasis-open.org/.../oasis-200401-wss-x509-token-profile-1.0\">" + keyIdentifer

    + "</wsse:KeyIdentifier>");

    xml.append("</wsse:SecurityTokenReference>");

    xml.append("</ds:KeyInfo>");

    xml.append("<CipherData>");

    xml.append("<CipherValue>" + token1 + "</CipherValue>");

    xml.append("</CipherData>");

    xml.append("</EncryptedKey>");

    xml.append("</ds:KeyInfo>");

    xml.append("<CipherData>");

    xml.append("<CipherValue>" + token2 + "</CipherValue>");

    xml.append("</CipherData>");

    xml.append("</EncryptedData>");

    xml.append("</Security>");

    xml.append("<a:MessageID>urn:uuid:" + java.util.UUID.randomUUID() + "</a:MessageID>");

    xml.append("<a:ReplyTo>");

    xml.append("<a:Address>www.w3.org/.../anonymous&lt;/a:Address>");

    xml.append("</a:ReplyTo>");

    xml.append("<a:To s:mustUnderstand=\"1\">" + url + "XRMServices/2011/Organization.svc</a:To>");

    xml.append("</s:Header>");

    return xml.toString();

      }

    }

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Unfortunately my internal Java interpretator/compilator doesn't work well so you can try to troubleshoot your code using Hibernate or other tool that can be used to troubleshoot Java code.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans