Php 按租户和应用程序ID创建Azure广告用户

Php 按租户和应用程序ID创建Azure广告用户,php,azure,azure-ad-graph-api,Php,Azure,Azure Ad Graph Api,如何使用PHP在Azure AD中通过客户端机密创建用户 我需要下面代码中的访问令牌来创建用户。要拥有这个令牌,我需要先登录。如何在没有任何登录的情况下自动创建用户 curl_setopt_array($curl, array( CURLOPT_URL => 'https://graph.microsoft.com/v1.0/users', CURLOPT_RETURNTRANSFER => true, CURLOPT_EN

如何使用PHP在Azure AD中通过客户端机密创建用户

我需要下面代码中的访问令牌来创建用户。要拥有这个令牌,我需要先登录。如何在没有任何登录的情况下自动创建用户

curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://graph.microsoft.com/v1.0/users',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS =>'{
                  "accountEnabled": true,
                  "displayName": "Adele Vance",                  
                  "userPrincipalName": "adelev2@xxx.net",
                  "passwordProfile" : {
                    "forceChangePasswordNextSignIn": true,
                    "password": "xWwvJ]6NMw+bWH-d"
          }
        }',
          CURLOPT_HTTPHEADER => array(
            "Authorization: Bearer $accessToken",
            "Content-Type: application/json"
          ),
        ));
您可以参考它,它使用不需要用户登录的守护进程,并使用获取访问令牌来调用以创建用户。您需要为应用程序授予User.ReadWrite.All
应用程序权限


特别感谢Carl提供了有用的链接,我使用了以下两个功能:

我通过调用
getToken
函数来接收令牌,并在
getToken
中使用它来创建一个没有任何先前登录的用户


function getToken() {
       
    $curl = curl_init();
    
    $dir = env('OAUTH_DIR_ID');
    $clientId = env('OAUTH_APP_ID');
    $secretKey = env('OAUTH_APP_PASSWORD');
    
    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://login.microsoftonline.com/$dir/oauth2/v2.0/token",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => "client_id=$clientId&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=$secretKey&grant_type=client_credentials",
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/x-www-form-urlencoded',
            'x-ms-gateway-slice=estsfd; stsservicecookie=estsfd'
        ),
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    echo $response;
    
}

function addUser($accessToken)
{
    try {

        $curl = curl_init();
        
        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://graph.microsoft.com/v1.0/users',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS =>'{
                  "accountEnabled": true,
                  "displayName": "Adele Vance",                  
                  "userPrincipalName": "adelev2@yoed.net",
                  "passwordProfile" : {
                    "forceChangePasswordNextSignIn": true,
                    "password": "xWwvJ]6NMw+bWH-d"
          }
        }',
          CURLOPT_HTTPHEADER => array(
            "Authorization: Bearer $accessToken",
            "Content-Type: application/json"
          ),
        ));
        
        $response = curl_exec($curl);
        
        curl_close($curl);
        
        var_dump($response); // Debug print
        exit();
        
        
    } catch (Error $ex) {
        $home = env('APP_URL');
        header("Location: $home/signin.php?err=" . $ex->getMessage());
        die();
    }
}

您好,如果我的答案对您有帮助,您可以将其作为答案接受(单击答案旁边的复选标记将其从灰色变为已填写)。您也可以接受自己的答案:)。您好@Carl。我用了你的,把它作为一个有用的答案。但是在你提供的链接的帮助下,正确的答案是我的。要接受我的回答,必须经过两天的提问。