How to create an event in a "Group Calendar" using MS Graph API (PHP plugin)
How to create an event in a "Group Calendar" using the MS Graph APIs? This is only possible using delegated permission from a Work or School Account. Would you please share the code to achieve that (using the PHP - new Graph())? Is it possible to do it completely silently, or login or consent pop-ups are involved?
HideAndSeek says he did it in the post: Microsoft Graph API: Group Calendar Events created by API are not sent to users Calendar but he does not share the code or explained how he did it.
Thanks
Answer
Solution:
To use APIs that only support delegated permissions, authentication + user consent is required. Below is a code sample assuming the access token variable is acquired with delegated permissions. I am using MS Graph PHP SDK
$accessToken = 'eyJ0...';
$graph = new Graph();
$graph->setAccessToken($accessToken);
$attendees = [];
array_push($attendees, [
'emailAddress' => [
'address' => 'user@domain.com',
'name' => 'Name'
],
'type' => 'required'
]);
$newEvent = [
'subject' => 'Sample Event Subject',
'attendees' => $attendees,
'start' => [
'dateTime' => '2021-05-12T22:00:00',
'timeZone' => 'Pacific Standard Time'
],
'end' => [
'dateTime' => '2021-05-12T23:00:00',
'timeZone' => 'Pacific Standard Time'
],
'body' => [
"contentType" => "HTML",
"content" => "Chat about new hire"
]
];
$response = $graph->createRequest('POST', '/groups/group-id/calendar/events')
->attachBody($newEvent)
->setReturnType(Model\Event::class)
->execute();
echo "Created event - {$response->getSubject()}.";
Here is a complete example that will help you get started. The Guide is here
Source