use strict; use warnings; use WWW::Curl::Easy; use Data::GUID; use Date::Manip; use XML::Simple; use Data::Dumper; sub make_guid { my $guidobj = Data::GUID->new; return $guidobj->as_string; } sub curl_request { my ($url,$data) = @_; my $headers = [ 'Connection: Keep-Alive', "Content-type: application/soap+xml; charset=UTF-8", "Content-length: " . length($data), ]; my $curl = WWW::Curl::Easy->new; $curl->setopt(CURLOPT_URL, $url); $curl->setopt(CURLOPT_TIMEOUT, 60); $curl->setopt(CURLOPT_SSL_VERIFYPEER, 0); $curl->setopt(CURLOPT_FOLLOWLOCATION, 1); $curl->setopt(CURLOPT_SSLVERSION, 3); $curl->setopt(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); $curl->setopt(CURLOPT_HTTPHEADER, $headers); $curl->setopt(CURLOPT_POST, 1); $curl->setopt(CURLOPT_POSTFIELDS, $data); my $response_body; $curl->setopt(CURLOPT_WRITEDATA,\$response_body); my $retcode = $curl->perform; return $retcode, $response_body; } my $guid = make_guid; my $reqguid = make_guid; my $region = 'urn:crmemea:dynamics.com'; # use your region my $username = 'youremailaddress'; my $password = 'yourpassword'; my $host = "yourhost.crm4.dynamics.com"; # use crm5 etc depending on region my $loginurl = 'https://login.microsoftonline.com/RST2.srf'; my $serviceurl = "https://$host/XRMServices/2011/Organization.svc"; my $OCPRequest = ' http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue urn:uuid:' . $guid . ' http://www.w3.org/2005/08/addressing/anonymous https://login.microsoftonline.com/RST2.srf '. UnixDate(ParseDate("now"), "%Y-%m-%dT%H:%M:%S").'Z ' . UnixDate(ParseDate("24 hours"), "%Y-%m-%dT%H:%M:%S") . 'Z ' . $username . ' ' . $password . ' '. $region .' http://schemas.xmlsoap.org/ws/2005/02/trust/Issue '; my ($retcode, $response); ($retcode, $response) = curl_request($loginurl, $OCPRequest); if ($retcode) { die("An error logging in\n"); } my $xml = XMLin($response); my $cipher1 = $xml->{"S:Body"}{'wst:RequestSecurityTokenResponse'}{'wst:RequestedSecurityToken'}{'EncryptedData'}{'ds:KeyInfo'}{'EncryptedKey'}{'CipherData' }{'CipherValue'}; my $cipher2 = $xml->{"S:Body"}{'wst:RequestSecurityTokenResponse'}{'wst:RequestedSecurityToken'}{'EncryptedData'}{ 'CipherData'}{'CipherValue'}; my $keyidentifier = $xml->{"S:Body"}{'wst:RequestSecurityTokenResponse'}{'wst:RequestedSecurityToken'}{'EncryptedData'}{ 'ds:KeyInfo'}{'EncryptedKey'}{'ds:KeyInfo'}{'wsse:SecurityTokenReference'}{'wsse:KeyIdentifier'}{'content'}; my $action = "Execute"; # or Retrieve, Create etc my $envelope_start = ' '; my $crmSoapRequestHeader = ' http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/' . $action . ' urn:uuid:' . $reqguid . ' http://www.w3.org/2005/08/addressing/anonymous ' . $serviceurl . ' ' .UnixDate(ParseDate("now"), "%Y-%m-%dT%H:%M:%S") . 'Z ' . UnixDate(ParseDate("24 hours"), "%Y-%m-%dT%H:%M:%S") . 'Z ' . $keyidentifier . ' ' . $cipher1 . ' ' . $cipher2 . ' '; my $retrieveMultipleRequestBodyTemplate =' WhoAmI '; my $envelope_end = ' '; ($retcode, $response) = curl_request($serviceurl,$envelope_start . $crmSoapRequestHeader . $retrieveMultipleRequestBodyTemplate . $envelope_end); if ($retcode == 0) { print("Transfer went ok\n"); print("Received response: $response\n"); } else { print("An error happened: $retcode \n"); print $envelope_start . $crmSoapRequestHeader . $retrieveMultipleRequestBodyTemplate . $envelope_end }