代码之家  ›  专栏  ›  技术社区  ›  Fllorinaaa

PHP连接到Microsoft Dynamics CRM

  •  0
  • Fllorinaaa  · 技术社区  · 2 年前

    我正尝试使用应用程序或客户端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版本不合适,因为服务器上有其他项目,我无法升级它。

    请原谅我的英语,我不是英语母语者。

    请帮帮我! 非常感谢。

    0 回复  |  直到 1 年前
        1
  •  0
  •   Fllorinaaa    2 年前

    我已经要求客户检查租户id。他给了我另一个,现在代码工作得很好。所以这真的是租户id。非常感谢您的回答和时间!

        2
  •  0
  •   DarkBee    2 年前

    您可以将需求分为两部分:

    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;
    

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

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

    推荐文章