PHP 连接到 Microsoft Dynamics CRM

问题描述 投票:0回答:2

我尝试使用应用程序或客户端 ID 以及基于客户端密钥的身份验证连接到 Microsoft Dynamics CRM。我有客户端 ID、客户端密码和租户 ID。

但它似乎无法连接,我收到下一个错误:“AADSTS90002:未找到租户‘xxx-xxx-xxx-xxx-xxx’。检查以确保您拥有正确的租户 ID 并登录到正确的云”。即使客户声称租户 ID 是正确的,所以我猜我在这里做错了什么。

这是代码:

$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);

然后我应该从 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";
    }
}

我不明白我做错了什么,互联网上只有几个例子。 我也尝试过 Alexa CRM,但我的 php 版本不适合,因为服务器上有其他项目,我无法升级它。

请原谅我的英语,我不是以英语为母语的人。

请帮忙! 谢谢!

php curl dynamics-crm crm
2个回答
0
投票

我已要求客户检查租户 ID。他给了我另一个,现在代码运行得很好。所以这确实是租户 ID.. 非常感谢您的回答和您的时间!


0
投票

您可以将您的要求分为两部分:

  1. 获取访问令牌
  2. 执行API

让我们从访问令牌开始,在您的示例中您使用 V1 端点,但在我的示例中我使用 V2 端点。

$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;

如果此代码有效,您将在 $response 中获取令牌,对其进行解码以便从 json 中获取 access_token 属性。

你的代码的第二部分对我来说看起来不错。

© www.soinside.com 2019 - 2024. All rights reserved.