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;Ā Ā Ā }Ā Ā
Extract the access token from the response URL.Ā The token will be retrieved once the page is redirected. The following code snippet shows retrieving the token.
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,Ā "Ā ")
);Ā Ā
} Ā
Get the Cloud Data using Graph API
In the AJAX call, set the required graph URL. In this case, we are trying to extract the calendar events of the logged in user, so the graph URL will be outlook calendar events. Set authorization bearer token (access token) in the header property. Using the get method, access the required events for the logged in user.
The graph URL for accessing logged in user events will be https://graph.microsoft.com/v1.0/me/calendar/events.
The following snippet shows the extraction of events using Graph API.
$.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.
In the next article, you will see a full sample code of extracting the Microsoft cloud data.Ā
Comments