5

我正在尝试从我保存所有用户信息的网站管理 ms 365 中的分发列表。我希望将用户与他们的个人电子邮件一起添加到分发列表中,因此无需成为 ms365 环境的成员。

$graph = new Graph();
$graph
->setBaseUrl("https://graph.microsoft.com/")
->setApiVersion("beta")
->setAccessToken($this->accessToken);
$group_id = 'xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx';
$data = json_encode(array(
    "GivenName" => "John",
    "Surname" => "Doe",
    "EmailAddresses" => array(
        array(
            "Address" => "email@example.com",
            "Name" => "John Doe"
        )
    )
));
        
$response = $graph->createRequest("post", "/groups/{$group_id}/members")
->addHeaders(array("Content-Type" => "application/json"))
->setReturnType(Model\User::class)
->attachBody($data)
->setTimeout("1000")
->execute();    

我得到的回应是:

未捕获的 GuzzleHttp\Exception\ClientException:客户端错误:POST https://graph.microsoft.com/beta/groups/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx/members导致400 Bad Request响应:{"error":{"code":"Request_BadRequest","message":"不支持的资源类型 'DirectoryObject' 用于操作 'Create'。","i (截断...)

进一步的错误堆栈:

C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0 
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\guzzle\src\Middleware.php(69): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response), NULL, Array, NULL) #1 
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) #2 
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(153): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), NULL) #3 
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\TaskQueue.php(48): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}() #4 
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(248): GuzzleHttp\Promise\TaskQueue->run(true) #5 
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(224): GuzzleHttp\Promise\Promise->invokeWaitFn() #6 
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(269): GuzzleHttp\Promise\Promise->waitIfPending() #7 
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(226): GuzzleHttp\Promise\Promise->invokeWaitList() #8 
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\promises\src\Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending() #9 
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\guzzlehttp\guzzle\src\Client.php(187): GuzzleHttp\Promise\Promise->wait() #10 
C:\xampp\htdocs\site\content\libraries\MicrosoftGraph\vendor\microsoft\microsoft-graph\src\Http\GraphRequest.php(270): GuzzleHttp\Client->request('post', 'beta/groups/aa2...', Array)

现在,从文档中我了解到将一个添加到列表/组的正确方法是使用:

“@odata.id”:“https://graph.microsoft.com/beta/directoryObjects/{id}”

使用它的用户 ID,但我想使用一个不存在的用户。在门户中,我可以以管理员身份执行此操作,但是图形 API 是否也可以解决此问题? https://docs.microsoft.com/en-us/graph/api/group-post-members?view=graph-rest-beta&tabs=http

4

1 回答 1

1

我从未使用过这个 api,但是,文档并没有说 {id} 应该引用现有用户,它说:

在请求正文中,提供 要添加的目录对象、用户或组对象的 ID 的 JSON 表示。

所以我猜如果你发布到目录对象它应该可以工作

于 2021-12-17T18:37:45.727 回答