[英]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.我正在尝试使用应用程序或客户端 ID 和基于客户端密码的身份验证连接到 Microsoft Dynamics CRM。 I have client id, client secret and Tenant Id.
我有客户 ID、客户机密和租户 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."但它似乎没有连接,我收到下一个错误:“AADSTS90002:找不到租户‘xxx-xxx-xxx-xxx-xxx’。检查以确保您拥有正确的租户 ID 并登录到正确的云。 “ eventhought the client claims that the tenant id is correct, so I am guessing I am doing something wrong here.
甚至认为客户声称租户 ID 是正确的,所以我猜我在这里做错了什么。
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:然后我应该从 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 inte.net.我不明白我做错了什么,互联网上只有几个例子。 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.
我也试过 Alexa CRM,但是我的 php 版本不合适,因为服务器上有其他项目,我无法升级它。
Please excuse my English, I am not a native English person.请原谅我的英语,我不是一个以英语为母语的人。
Please help!请帮忙! Thank you!
谢谢你!
You can divide your requirement in two parts:您可以将您的要求分为两部分:
Let's begin with the Access Token, in your example you use the V1 endpoint but in my example I use the V2 endpoint.让我们从访问令牌开始,在您的示例中您使用 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;
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.如果此代码有效,您将在 $response 中获取令牌,您对其进行解码以便从 json 获取 access_token 属性。
The second part of your code looks ok to me.您的代码的第二部分对我来说看起来不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.