Introduction
In this article series, we are focusing on accessing the Office 365 and Microsoft Cloud Services data, using graph REST API calls.
In this article, let us look at the samples for accessing the office 365 outlook events and outlook messages with the help of Microsoft graph API.
Microsoft Graph exposes Office 365 and other Microsoft cloud services data like outlook mail, outlook calendar, one drive, tasks, groups, SharePoint, etc. through single endpoint URL (https://graph.microsoft.com).
In my previous articles, you will have seen the basic introduction, app registration process, getting access token for the authentication and accessing Microsoft Services data over graph API, using AJAX call.
Get data
There will be two calls to access Microsoft Cloud data. First call is to acquire the access token for any further authorizations and the second call is to access the data with the access token.
- On page load, using the condition; check if the URL consists of an access token parameter. Normally on the first page load, there will not be any parameters present on the URL).
- If an access token is not available, acquire the access token with the help of an authentication Server URL with the necessary parameters. This step was explained in my previous article with detailed steps. On success, the page will be redirected to the redirected URL specified with an access token.
- If the token is available on the URL, decode the response URL and extract the token.
- Once the token is extracted and available, access Microsoft Cloud data with an appropriate graph URL. The access token, which has been acquired, should be passed in the header for the authorizations while accessing the data with graph URL.
Note
The acquired token will be valid for one hour.
JavaScript code snippet given below shows the entire functionality to retrieve the calendar events with the explained authentication model given above.
- $(document).ready(function(){
- var token = getParameterByName['access_token'];
-
- if(token == null || token == undefined){
-
- requestToken();
- }
- else{
-
- $.ajax({
- url: "https://graph.microsoft.com/v1.0/me/messages",
- type: "GET",
- headers: { "Authorization": "Bearer "+token },
- success: function (data) {
- var messages = reactHandler.state.messages;
-
- for(var i=0;i<data.value.length;i++){
- if(data.value[i].subject != null){
- messages.push({
- subject:data.value[i].subject,
- weblink:data.value[i].webLink
- })
- }
-
- }
- reactHandler.setState({
- messages: messages
- });
- },
- error: function (sender, args) {
- console.log("error");
- }
- });
-
- }
- });
-
-
- 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, " "));
- }
-
-
- function requestToken() {
- var clientId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
- var replyUrl = 'https://nakkeerann.sharepoint.com';
- var resource = "https://graph.microsoft.com";
- var authServer = 'https://login.microsoftonline.com/nakkeerann.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;
- }
Summary
Thus, you have seen accessing Microsoft Cloud data, using Microsoft graph URL on SharePoint. In this sample, Office 365 Outlook calendar events are retrieved.