Sourav Kuila
Sourav Kuila

Reputation: 23

How to get 'calendar' labels and 'calendar items' using Microsoft Graph REST API v1.0

I'am developing a Outlook Web Add-In using Microsoft Graph REST API v1.0. After a lot of searches include Microsoft's documentations and finally able to authorize and get 'access_token' using the Add-In, but badly stuck to get user's 'calendar' and also 'calendar events' include recurring events using the bellow code

 //// for all calendar label
 var _token="xyz..123";
 $.ajax({
    url: "https://graph.microsoft.com/v1.0/me/calendars",
    headers: {
        'outlook.body-content-type': 'text/html',
        'Authorization': 'Bearer ' + token
    },
    type: 'GET'
}).done(function (data) {
    //// success
}).fail(function (error) {
    //// error
});


//// for calendar events
var _token="xyz..123";
 $.ajax({
    url: "https://graph.microsoft.com/v1.0/me/calendar/events",
    headers: {
        'outlook.body-content-type': 'text/html',
        'Authorization': 'Bearer ' + token
    },
    type: 'GET'
}).done(function (data) {
    //// success
}).fail(function (error) {
    //// error
});

ref : https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_list_events

I'am getting this error in response

{
"error": {
    "code": "InvalidAuthenticationToken",
    "message": "CompactToken parsing failed with error code: 80049217",
    "innerError": {
        "request-id": "55ea48c0-5d10-423b-9f41-d48d5b1d9454",
        "date": "2018-10-01T14:41:05"
      }
   }
}

Please point me out where I'm doing wrong? Thanks

UPDATE

As suggested by @Lina I tried this method and switch to Outlook REST API v2.0

Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (result) {
    if (result.status === "succeeded") {
        var accessToken = result.value;
        // Use the access token
    } else {
        // Handle the error
    }
});

got the accessToken = "123..abc"; After that I tried to access calendar and events in this way

// get calendars
$.ajax({
    url: 'https://outlook.office.com/api/v2.0/me/calendars' ,
    dataType: 'json',
    headers: { 'Authorization':'Bearer ' +access_token}
  }).done(function (items) {
    //sucess;
  }).fail(function (error) {
    //error
});

// get events
$.ajax({
    url: 'https://outlook.office.com/api/v2.0/me/events' ,
    dataType: 'json',
    headers: { 'Authorization':'Bearer ' +access_token}
  }).done(function (items) {
    //sucess;
  }).fail(function (error) {
    //error
});

In the above both request I got this error

"{"error":{"code":"ErrorAccessDenied","message":"The api you are trying to access does not support item scoped OAuth."}}"

related:Access to Outlook RestAPI from an Outlook web Add-in

Help me out what I'm missing. Is there any 'permission' or 'parameters'issue? Thanks

Upvotes: 0

Views: 445

Answers (1)

Lina
Lina

Reputation: 291

If you use the getAccessTokenAsync method to get the 'access_token', you should use the request shown below:

Get a collection of series master and single instance events from the user's primary calendar (../me/events) or from a different calendar. To get expanded event instances, you can get the calendar view or get the instances of an event.

GET https://outlook.office.com/api/v2.0/me/events
GET https://outlook.office.com/api/v2.0/me/calendars/{calendar_id}/events

For more information, please see the link below:

Using the Calendar REST API

Upvotes: 0

Related Questions