skip to Main Content

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: $leadIdn";
        echo "Full Name: $fullNamen";
        echo "Email: $emailn";
        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!

2

Answers


  1. Chosen as BEST ANSWER

    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!


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search