alek kowalczyk
alek kowalczyk

Reputation: 4936

Outlook JS Add-In + Outlook REST API

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:

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.

Upvotes: 0

Views: 1500

Answers (4)

Jay Harry
Jay Harry

Reputation: 221

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

Upvotes: 0

Shyam sundar shah
Shyam sundar shah

Reputation: 2523

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

Upvotes: 0

user2133516
user2133516

Reputation: 33

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.

Upvotes: 0

Benoit Patra
Benoit Patra

Reputation: 4545

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.

Upvotes: 1

Related Questions