0

How can I get events from Office 365 Calendar, from REST?

I create an application in Azure, I have Token from this request:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?
  client_id=[app_id from AZURE]
  &response_type=code
  &redirect_uri=https://mypage_domain.com
  &response_mode=query
  &scope=https%3A%2F%2Fgraph.microsoft.com%2Fcalendars.read%20
  &state=12345

Next i try get Events:

$ch = curl_init();
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$postFields = 'grant_type=authorization_code&code='.$authCode.'&redirect_uri='.$redirectUri.'&scope=openid&client_id='.$clientId.'&client_secret='.$clientSecret;

curl_setopt($ch, CURLOPT_URL,'https://login.microsoftonline.com/common/oauth2/v2.0/token');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$serverOutput = curl_exec ($ch);
curl_close ($ch);
$jsonOutput = json_decode($serverOutput, true);

$chUser = curl_init();
$headersUser[] = 'Authorization: Bearer '.$jsonOutput['id_token'];
curl_setopt($chUser, CURLOPT_URL,'https://graph.microsoft.com/v1.0/me/events/');
curl_setopt($chUser, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($chUser, CURLOPT_HTTPHEADER, $headersUser);
$serverOutputUser = curl_exec($chUser);
curl_close($chUser);

But I get an error: Access token validation failure.

What did I do wrong? I found examples for Laravel but I need simple, universal code. I need use this in Wordpress to write events from calendar for page users.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
rask
  • 345
  • 1
  • 3
  • 6

1 Answers1

1

You are indicating wrong authorization header. In fact, the authorization header uses bearer token which has the structure: Bearer <access_token>.

$headersUser[] = 'Authorization: Bearer '.$jsonOutput['**access_token**'];
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
EagleDev
  • 1,754
  • 2
  • 11
  • 31
  • Thank you for your answer. I changed to 'access_token' but it still does not work. I have the same error. Maybe because I'm getting the token from the domain "login.microsoftonline.com", and tries to use it on the domain "graph.microsoft.com" ? – rask Feb 28 '18 at 08:18
  • No, getting from login.microsoftonline.com is correct. Graph.microsoft.com is just scope you need to have access before you can retrieve calendar information. Could you debug to see what was wrong? Could you post the entire request body? – EagleDev Feb 28 '18 at 09:15
  • I have this error: `{ "error": { "code": "InvalidAuthenticationToken", "message": "Access token validation failure.", "innerError": { "request-id": "8bda012b-dddc-4a4a-9ad8-36fbeb843d78", "date": "2018-02-28T13:39:39" } } }`. Maybe I have wrong scope? `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=[cliend_id_from_app]&redirect_uri=[redirect_url]&response_type=code&scope=https://outlook.office.com/calendars.read` – rask Feb 28 '18 at 13:48
  • 1
    Yes look at this for scope of outlook https://stackoverflow.com/q/40381513/9330826 – EagleDev Feb 28 '18 at 14:16
  • I change scope to `https://outlook.office.com/calendars.read.shared` and works. Thank you for your help. – rask Mar 02 '18 at 09:14