1

Good morning all,

I'm trying to create a lead entity in Microsoft Dynamics NAV 365, from a php CURL script. However I keep getting a "HTTP Error 401 - Unauthorised: Access is denied" in my CURL response. I can however, create a lead via the web interface fine.

I've created my object from the lead entity type as described on the MSDN docs website.

Below is my code:

$lead = array('person' =>
    array(
        'topic'                 => 'WEB LEAD',
        'name'                  => $fullname,
        'firstname'             => $firstname,
        'lastname'              => $lastname,
        'companyname'           => $company,
        'telephone1'            => $telephone,
        'emailaddress1'         => $email,
        'description'           => $comment,
    ),
);

$dynamics =  $url . '/api/data/v8.2/leads';
$ch = curl_init($dynamics);

$options = array(
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json; charset=utf-8',
        'OData-MaxVersion: 4.0',
        'OData-Version: 4.0',
        'Accept: application/json',
    ),
    CURLOPT_HEADER => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_USERPWD, 'username:password',
    CURLOPT_POSTFIELDS => json_encode($lead),
);

curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
Graeme Leighfield
  • 2,825
  • 3
  • 23
  • 38
  • I think that code is just sending a user/pass combination as basicAuth. It'd be astonished if Dynamics allowed basic authentication. Maybe read https://msdn.microsoft.com/en-us/library/gg327838.aspx - I think you need to use OAuth. – ADyson Mar 07 '17 at 12:31
  • Could you elaborate on your authentication mechanism in Dynamics? Are you using CBA or IFD? – Matt L Mar 07 '17 at 15:17
  • Hi @MattL - I'm afraid I don't know :( Could you point me in the direction of what they mean? I'm struggling to find any viable documentation for PHP with Dynamics. – Graeme Leighfield Mar 07 '17 at 17:50
  • 1
    @GraemeLeighfield Here is a link explaining a bit about CBA and IFD. https://community.dynamics.com/crm/b/1984crm/archive/2016/01/19/ifd-claims-adfs-what-now If you are using either of those as an authentication method, then you will need to modify your PHP script to adapt. I have not used PHP for many years, so I'm afraid that's all I can help with. – Matt L Mar 07 '17 at 18:18
  • Alexa CRM's PHP toolkit may be helpful: https://alexacrm.com/toolkit/. The code is here: https://github.com/AlexaCRM/php-crm-toolkit – Aron Jul 27 '17 at 13:09
  • could you solve this with basicAuth? – Dominik Mayrhofer Sep 03 '21 at 05:46

2 Answers2

0

You need previously get authorization token from AAD

After you get all token you need to add the authorization in the request http header

CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json; charset=utf-8',
    'OData-MaxVersion: 4.0',
    'OData-Version: 4.0',
    'Accept: application/json',
    'Authorization: <put the token here completly with name>',
),
  • did you get solution I am also looking for this one. Any one from you please help me on this. – Denis Bhojvani Mar 29 '18 at 12:02
  • Hi Denis. You need first do a connection to azure active directory if you use the cloud system to get token of authorization, the web page if I remember it's login.windows I don't remember specific web page and send the username and password for see if valid, after that you can send the content to d3fO with the token check this image https://learn.microsoft.com/en-us/dynamics365/unified-operations/dev-itpro/data-entities/media/services-authentication.png – Eduardo Rotundo Mar 30 '18 at 13:16
  • 1
    Thanks for your help. But I also found one library which is work fine now. https://github.com/AlexaCRM/php-crm-toolkit – Denis Bhojvani Mar 30 '18 at 13:31
0

Adding curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); solved the problem for me. The whole source looks like this:

$ch = curl_init();

if(!empty($parameters)){ //Add params (array) to your request if you want
    $url .= "?".http_build_query($parameters);
}

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);

$login = sprintf('%s:%s', self::USERNAME, self::PASSWORD);
curl_setopt($ch, CURLOPT_USERPWD, $login);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Connection: Keep-Alive',
        'Accept: application/json',
        'Content-Type: application/json; charset=utf-8'
    )
);

$response = curl_exec($ch);
$response_info = curl_getinfo($ch);
curl_close($ch);