Search code examples
phpcurldynamics-crmcrm

PHP Connect to Microsoft Dynamics CRM


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!


Solution

  • 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!