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</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</a:Address>");
xml.append("</a:ReplyTo>");
xml.append("<a:To s:mustUnderstand=\"1\">login.microsoftonline.com/RST2.srf</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</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</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</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();
}
}