Cannot add to another users calendar

2019-07-31 10:15发布

问题:

I am attempting to allow a user add items to the calendars of other users. A user logs in and get the token as follows

const AUTHORIZE_ENDPOINT = '/oauth2/v2.0/authorize';
const TOKEN_ENDPOINT     = '/oauth2/v2.0/token';
const SCOPES             = 'profile openid email User.Read Calendars.ReadWrite Calendars.Read Calendars.Read.Shared Calendars.ReadWrite Calendars.ReadWrite.Shared';


$graph = new Graph();
$graph->setAccessToken($token);
$response = $graph->createRequest("GET", "/me")->setReturnType(Model\User::class)->execute(); 

The logged in user can add to their own calendar using

$request = $graph->createRequest("post", '/me/events');
$request->attachBody($data);
$response = $request->execute(); 

But, when I try to add to another user with

$request = $graph->createRequest("post", '/anotheruser/events');
$request->attachBody($data);
$response = $request->execute(); 

I get the message

Resource not found for the segment

Have done the admin auth consent, so all should be fine.

Any suggestions?

回答1:

If you want to access another user's data you have to use the following url:

/users/{id | userPrincipalName}

Your request was just in the wrong form and you ended up sending a request to an non existing resource, thus Graph didn't know what to do.
In your case you just need to prepend /users (for more information see documentation).

So your request could look like this:

$request = $graph->createRequest("post", '/users/anotheruser/events');

Keep in mind that if you are logged in as a user (token on behalf of a user), the calendar you try to access to must have been shared with the logged in user. Otherwise it will fail due to missing privileges as Graph only allows to edit Calendars that are shared with the user. You also need the Permissions Calendars.Read.Shared and/or Calendars.ReadWrite.Shared (which you seem to already have aquired).
Calendar sharing is unnecessary if you gain access without a user as you then automatically have full access to all users.

Currently the Graph API only supports access to shared Calendars but no operations to edit/change the sharing status or see which Calendars are shared with a user. However you can change the sharing status manually over outlook or the powershell.



回答2:

Url should be something like Users('anotheruser')/events. Since you are directly saying anotheruser/events. The service is not recognizing another user as a valid segment and throwing the error.