Outlook JS Add-In + Outlook REST API

1.4k views Asked by At

I want my Outlook 365 integration to use both, the Outlook JS Add-In API, and the Outlook REST API.

But as I see it, it has separate permission/credential handling:

  • The Outlook Add-In API has following permissions: Restricted, ReadItem, ReadWriteItem, or ReadWriteMailbox
  • The Outlook REST API has full OAuth2 support with scopes, e.g. the offline_access scope which interest me the most.

What I wan't is to use the Outlook Add-In to give the user additional UI elements, and use the Outlook REST API to keep my data in sync with Outlook data.

Is it possible without forcing the user to give consent twice? That is firstly by giving consent for the Add-In when installing it, and then consent for our app which uses the Outlook REST API for sync jobs.

4

There are 4 answers

2
Benoit Patra On BEST ANSWER

To my knowledge, it is not possible now without asking the end user to complete a second, OAUTH based, authentication flow.

Note that you can use EWS (Exchange Web Services, which is not REST) without a secondary authentication flow, if you need to access data not provided by Office.js

Have a look at getUserIdentityTokenAsync or makeEwsRequestAsync here.

0
Jay Harry On

You can obtain the Outlook REST API's access token from in your outlook JS Plug-in using the
Office.context.mailbox.getCallbackTokenAsync method as show below

Office.context.mailbox.getCallbackTokenAsync({isRest: true}, function(result){
  if (result.status === "succeeded") {
    var accessToken = result.value;

    // Use the access token.
    getCurrentItem(accessToken);
  } else {
    // Handle the error.
  }
});

Source: Get an access token

2
user2133516 On

You don't need a second Auth flow, you can use Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (result) ... to get the access token, then you can consume the Calendar REST API using JQuery/Ajax for instance.

0
Shyam sundar shah On

you can use Office.js and you can get token from it and token is used for performing ajax call. I am giving example here

var _mailbox = Office.context.mailbox;
_mailbox.getCallbackTokenAsync({ isRest: true }, function (result) {
    if (result.status === Office.AsyncResultStatus.Succeeded) {
       var  accessToken = result.value;

         var userDetail;
     var url = Office.context.mailbox.restUrl+ "/v2.0/me";
   try {
    $.ajax({
        type: 'GET',
        url: url,
        contentType: "application/json",
        headers: { 'Authorization': 'Bearer ' + accessToken },
        async: false,
        success: function (data) {
            userDetail = data;
        },
        error: function (textStatus, errorThrown) {

            userDetail = null;//doesnt goes here
        }

    });
}
catch (error)
{
    userDetail = null;
    writeLog(error);
}
  }
  }

Above code give current user detail including first Name , email adddress and many more, You can perform several other operation such create folder, move email ,delete email and many more