我正尝试使用应用程序或客户端Id和基于客户端密码的身份验证连接到Microsoft Dynamics CRM。我有客户id、客户机密和租户id。
但它似乎没有连接,我收到了下一个错误:“AADSTS90002:找不到租户'xxx-xxx-xxx-xxxx'。请检查以确保您有正确的租户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);
然后我应该从客户关系管理部门得到线索:
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版本不合适,因为服务器上有其他项目,我无法升级它。
请原谅我的英语,我不是英语母语者。
请帮帮我!
非常感谢。