Can we use Graph API for create an event without sign-in?

Sagar Chauhan 0 Reputation points
2024-07-02T11:14:50.5066667+00:00

My purpose here is to create an event from my website and share the meeting joining link to the users but it's required delegated permission. Is there any alternative way?

<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Users\Item\Events\EventsRequestBuilderPostRequestConfiguration;
use Microsoft\Graph\Generated\Models\Event;
use Microsoft\Graph\Generated\Models\ItemBody;
use Microsoft\Graph\Generated\Models\BodyType;
use Microsoft\Graph\Generated\Models\DateTimeTimeZone;
use Microsoft\Graph\Generated\Models\Location;
use Microsoft\Graph\Generated\Models\Attendee;
use Microsoft\Graph\Generated\Models\EmailAddress;
use Microsoft\Graph\Generated\Models\AttendeeType;
use Microsoft\Graph\Generated\Models\OnlineMeetingProviderType;


$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);

$requestBody = new Event();
$requestBody->setSubject('Let\'s go for lunch');
$body = new ItemBody();
$body->setContentType(new BodyType('hTML'));
$body->setContent('Does noon work for you?');
$requestBody->setBody($body);
$start = new DateTimeTimeZone();
$start->setDateTime('2017-04-15T12:00:00');
$start->setTimeZone('Pacific Standard Time');
$requestBody->setStart($start);
$end = new DateTimeTimeZone();
$end->setDateTime('2017-04-15T14:00:00');
$end->setTimeZone('Pacific Standard Time');
$requestBody->setEnd($end);
$location = new Location();
$location->setDisplayName('Harry\'s Bar');
$requestBody->setLocation($location);
$attendeesAttendee1 = new Attendee();
$attendeesAttendee1EmailAddress = new EmailAddress();
$attendeesAttendee1EmailAddress->setAddress('samanthab@contoso.com');
$attendeesAttendee1EmailAddress->setName('Samantha Booth');
$attendeesAttendee1->setEmailAddress($attendeesAttendee1EmailAddress);
$attendeesAttendee1->setType(new AttendeeType('required'));
$attendeesArray []= $attendeesAttendee1;
$requestBody->setAttendees($attendeesArray);

$requestBody->setAllowNewTimeProposals(true);
$requestBody->setIsOnlineMeeting(true);
$requestBody->setOnlineMeetingProvider(new OnlineMeetingProviderType('teamsForBusiness'));
$requestConfiguration = new EventsRequestBuilderPostRequestConfiguration();
$headers = [
	'Prefer' => 'outlook.timezone="Pacific Standard Time"',
];
$requestConfiguration->headers = $headers;


$result = $graphServiceClient->me()->events()->post($requestBody, $requestConfiguration)->wait();

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,289 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 42,531 Reputation points
    2024-07-04T08:37:57.7333333+00:00

    Hi @Sagar Chauhan

    Of course, you can use an unattended context, you just need to create a graphClient instance using the client credentials flow.

    <?php 
    
    use Microsoft\Graph\GraphServiceClient; 
    use Microsoft\Graph\Generated\Users\Item\Events\EventsRequestBuilderPostRequestConfiguration; use Microsoft\Graph\Generated\Models\Event; use Microsoft\Graph\Generated\Models\ItemBody; 
    use Microsoft\Graph\Generated\Models\BodyType; 
    use Microsoft\Graph\Generated\Models\DateTimeTimeZone; 
    use Microsoft\Graph\Generated\Models\Location; 
    use Microsoft\Graph\Generated\Models\Attendee; 
    use Microsoft\Graph\Generated\Models\EmailAddress; 
    use Microsoft\Graph\Generated\Models\AttendeeType;
    
    $scopes = ['https://graph.microsoft.com/.default'];
    // Values from app registration
    $clientId = 'YOUR_CLIENT_ID';
    $tenantId = 'YOUR_TENANT_ID';
    $clientSecret = 'YOUR_CLIENT_SECRET';
    // Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext
    $tokenContext = new ClientCredentialContext(
        $tenantId,
        $clientId,
        $clientSecret);
    
    $graphClient = new GraphServiceClient($tokenContext, $scopes);
    
    $requestBody = new Event();
    $requestBody->setSubject('Let\'s go for lunch');
    $body = new ItemBody();
    $body->setContentType(new BodyType('hTML'));
    $body->setContent('Does noon work for you?');
    $requestBody->setBody($body);
    $start = new DateTimeTimeZone();
    $start->setDateTime('2024-07-4T12:00:00');
    $start->setTimeZone('Pacific Standard Time');
    $requestBody->setStart($start);
    $end = new DateTimeTimeZone();
    $end->setDateTime('2024-07-4T14:00:00');
    $end->setTimeZone('Pacific Standard Time');
    $requestBody->setEnd($end);
    $location = new Location();
    $location->setDisplayName('Harry\'s Bar');
    $requestBody->setLocation($location);
    $attendeesAttendee1 = new Attendee();
    $attendeesAttendee1EmailAddress = new EmailAddress();
    $attendeesAttendee1EmailAddress->setAddress('samanthab@contoso.com');
    $attendeesAttendee1EmailAddress->setName('Samantha Booth');
    $attendeesAttendee1->setEmailAddress($attendeesAttendee1EmailAddress);
    $attendeesAttendee1->setType(new AttendeeType('required'));
    $attendeesArray []= $attendeesAttendee1;
    $requestBody->setAttendees($attendeesArray);
    
    $requestBody->setAllowNewTimeProposals(true); 
    $requestBody->setTransactionId('
    $requestConfiguration = new EventsRequestBuilderPostRequestConfiguration(); 
    $headers = [ 
         'Prefer' => 'outlook.timezone="Pacific Standard Time"', 
    ]; 
    $requestConfiguration->headers = $headers; 
    
    
    $result = $graphClient->users()->byUserId('user-id')->events()->post($requestBody, 
    $requestConfiguration)->wait();
    

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.