0

I am trying to connect to Microsoft Dynamics CRM using an Application or Client Id and a Client Secret based authentication. I have client id, client secret and Tenant Id.

But it does not seem to connect, I get the next error: "AADSTS90002: Tenant 'xxx-xxx-xxx-xxx-xxx' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud." eventhought the client claims that the tenant id is correct, so I am guessing I am doing something wrong here.

Here is the code:

$clientId = 'xxx-xxx-xx-xx-xx';
$clientSecret = 'test';
$resource = 'https://test.crm4.dynamics.com';
$tokenEndpoint = 'https://login.microsoftonline.com/xxx-xxx-xxx-xxx-xxx/oauth2/token';

// Prepare the request body
$params = array(
    'grant_type' => 'client_credentials',
    'client_id' => $clientId,
    'client_secret' => $clientSecret,
    'resource' => $resource
);
$query = http_build_query($params);

// Create the cURL request
$ch = curl_init($tokenEndpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);
curl_close($ch);

// Parse the response
$data = json_decode($response, true);
print_r($response);

Then i should get the leads from the crm:

if (isset($data['access_token'])) {
    $accessToken = $data['access_token'];

    // Use the access token to make API requests
    // For example, retrieve leads
    $leadsEndpoint = 'https://test.crm4.dynamics.com/api/data/v9.1/leads';
    $headers = array(
        'Authorization: Bearer ' . $accessToken,
        'Accept: application/json',
        'OData-MaxVersion: 4.0',
        'OData-Version: 4.0',
    );

    $ch = curl_init($leadsEndpoint);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

    // Process the leads response
    $leads = json_decode($response, true);
    foreach ($leads['value'] as $lead) {
        // Process each lead record as needed
        $leadId = $lead['leadid'];
        $fullName = $lead['fullname'];
        $email = $lead['emailaddress1'];

        echo "Lead ID: $leadId\n";
        echo "Full Name: $fullName\n";
        echo "Email: $email\n";
        echo "\n";
    }
} else {
    // Handle authentication error
    if (isset($data['error_description'])) {
        echo "Authentication Error: " . $data['error_description'];
    } else {
        echo "Authentication Error";
    }
}

I do not understand what I am doing wrong, there are just a few examples on the internet. I have also tried Alexa CRM, but my php version is not suitable, as I cannot upgrade it because of the other projects on the server.

Please excuse my English, I am not a native English person.

Please help! Thank you!

Fllorinaaa
  • 81
  • 1
  • 7

1 Answers1

0

You can divide your requirement in two parts:

  1. Get the Access Token
  2. Execute the API

Let's begin with the Access Token, in your example you use the V1 endpoint but in my example I use the V2 endpoint.

$url = 'https://test.crm4.dynamics.com';
$clientid = 'b599fdfa-xxxx-xxxx-xxx-d9b263887b55';
$clientsecret = 'test';
$tenantid = '5f2b5560-xxxx-xxxx-xxxx-6e287406adf6';

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://login.microsoftonline.com/'.$tenantid.'/oauth2/v2.0/token',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => 'grant_type=client_credentials&client_id='.$clientid.'&client_secret='.$clientsecret.'&scope='.$url.'/.default',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/x-www-form-urlencoded'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

If this code works you will get the token inside the $response, you decode it in order to get the access_token property from the json.

The second part of your code looks ok to me.

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Guido Preite
  • 14,905
  • 4
  • 36
  • 65
  • I get the same error. I will ask them to check again for the Tenant Id. Thank you! – Fllorinaaa Jun 08 '23 at 06:37
  • @Fllorinaaa I had time to test, the code I posted works with a trial. In addition to the tenantid, check if the app registration has the right permissions inside Dynamics (it should be added as application user) – Guido Preite Jun 08 '23 at 12:11
  • Hello! I have requested the client to check the tenant id. He gave me another one, and now the code works great. So it really was the tenant id.. Thank you very much for your answers and for your time! – Fllorinaaa Jun 09 '23 at 07:58