Introduction
In this article series, we are focusing on accessing the Office 365 and Microsoft cloud services data using Graph REST API calls. Here, in this article, let us see how to access the Office 365 Outlook calendar events with the help of Microsoft Graph API.
Microsoft Graph exposes Office 365 and other Microsoft cloud services data like Outlook mail, Outlook calendar, OneDrive, tasks, groups, SharePoint, etc. through single endpoint URL (https://graph.microsoft.com).
In my previous article, you have seen the basic introduction and app registration process for accessing the Microsoft Graph data.
The data can be extracted by the following flow.
- Get access token for authentication.
- Access the Microsoft services data over graph API.
Acquire Access Token
There are two ways to get an access token. Here, I am only focusing on single approach. In this approach, we will be getting the access token using authentication server URL (https://login.microsoftonline.com/<yourdomain.onmicrosoft.com>/oauth2/authorize) with GET method.
The parameters passed for getting the token are,
- Client ID
- Response type
- Resource URL
- Redirect URL
Fire the constructed URL in the browser using window.location script, and the page will be redirected to the redirect URL specified. In the response URL, the access token will be available. The response URL will be redirect URL specified for redirection.
- function requestToken() {
- var clientId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
- var replyUrl = 'https://yourdomain.sharepoint.com';
- var resource = "https://graph.microsoft.com";
- var authServer = 'https://login.microsoftonline.com/yourdomain.onmicrosoft.com/oauth2/authorize?';
- var responseType = 'token';
- var url = authServer +
- "response_type=" + encodeURI(responseType) + "&" +
- "client_id=" + encodeURI(clientId) + "&" +
- "resource=" + encodeURI(resource) + "&" +
- "redirect_uri=" + encodeURI(replyUrl);
- window.location = url;
- }
- var token = getParameterByName['access_token'];
- function getParameterByName(name, url) {
- if (!url) url = window.location.href;
- name = name.replace(/[\[\]]/g, "\\$&");
- var regex = new RegExp("[?&]" + name + "(=([^]*)|&|#|$)"),
- results = regex.exec(url);
- if (!results) return null;
- if (!results[2]) return '';
- return decodeURIComponent(results[2].replace(/\+/g, " "));
- }
The graph URL for accessing logged in user events will be https://graph.microsoft.com/v1.0/me/calendar/events.
- $.ajax({
- url: "https://graph.microsoft.com/v1.0/me/messages",
- type: "GET",
- headers: { "Authorization": "Bearer "+token },
- success: function (data) {
- for(var i=0;i<data.value.length;i++){
- // extract the event subject, web link, etc.
- }
- },
- error: function (sender, args) {
- console.log("error");
- }
- });
Summary
Thus, you learned accessing the Microsoft cloud data using the Microsoft graph URL on SharePoint. In this sample, Office 365 Outlook calendar events are retrieved. Likewise, other Microsoft cloud data can also be accessed.

PrashanthPosted Jun 29, 2017, 7:19 PM
Hi Nakkeeran , any alternate using "window.location = url;" to get access_token ?