user813813
user813813

Reputation: 355

How do I add an event to another users calendar in Office 365 using PHP?

I am using Microsoft Graph and wish to give users the ability to add items to other users calendar though a an app we are writing.

I am using the following code, and can add to my own calendar.

$subject = 'Subject';
$body = 'Body content';
$start = time();
$end = time() + (30 * 60);
$startStr = date('Y-m-d', $start) . 'T' . date('H:i:s', $start) . ".0000000";
$endStr = date('Y-m-d', $end) . 'T' . date('H:i:s', $end) . ".0000000";

$data = array();
$data['subject'] = $subject;
$data['body'] =  array();
$data['body']['content'] = $body;
$data['start'] = array();
$data['start']['dateTime'] = $startStr;
$data['start']['timeZone'] = 'UTC';
$data['end'] = array();
$data['end']['dateTime'] = $endStr;
$data['end']['timeZone'] = 'UTC';

echo 'subject: ' . $subject . '<br>';
echo 'body: ' . $body . '<br>';
echo 'start: ' . $startStr . '<br>';
echo 'end: ' . $endStr . '<br>';

$token = $_SESSION['access_token'];
$graph = new Graph();
$graph->setAccessToken($token);
$request = $graph->createRequest("post", '/me/calendar');
$request->attachBody($data);
$response = $request->execute(); 

When I change the createRequest to be

$request = $graph->createRequest("post", '/xxxx/calendar');

where xxxx is another user name, I am getting the following response

400 Bad Request

The permissions I am using to login are

profile openid email User.Read Calendars.ReadWrite Calendars.Read Calendars.Read.Shared Calendars.ReadWrite Calendars.ReadWrite.Shared

What else do I need to do in order to send items to another users calendar?

Upvotes: 1

Views: 792

Answers (1)

Michael Mainer
Michael Mainer

Reputation: 3465

You are missing the permissions to act on another user's calendar. You have a few ways that you can do this:

  1. You can send a meeting invite to the user. This will work for most users since the default behavior is for meeting invites to automatically be added to a user's calendar. Just create a meeting with the target user as an attendee.
  2. Since you have Calendar.ReadWrite.Shared, the target user needs to share their calendar with you.
  3. Have application permissions be set for Calendar.ReadWrite.

The choice of which one is most appropriate depends on the requirements, how the application is structured, and who is the actor that is making a change.

Upvotes: 3

Related Questions