Can't access Outlook REST API from within Outlook Add in

0

I have created an Outlook addin project using yo office command. Inside it I'm trying to fetch list of mails, using the below code:

// Get access token
  Office.context.mailbox.getCallbackTokenAsync({isRest: true}, function(result){
    if (result.status === "succeeded") {
      var accessToken = result.value;
      console.log('Access token : ' + accessToken);
    } else {
      console.log('Unable to get access token :(');
    }

    // Get item id
    var itemId = Office.context.mailbox.convertToRestId(
      Office.context.mailbox.item.itemId,
      Office.MailboxEnums.RestVersion.v2_0
    );
    console.log('Item Id : ' + itemId);

    // Get messages now
    var messagesUrl = Office.context.mailbox.restUrl + 'v2.0/me/messages/' + itemId;
    console.log('Querying url : ' + messagesUrl);
    $.ajax({
      url: messagesUrl,
      dataType: 'json',
      headers: { 'Authorization': 'Bearer ' + accessToken }
    }).done(function(item){
      console.log(item);
    }).fail(function(error){
      console.log(error);
    });
  });

The problem is that on running the addin, I get the following error:

SEC7120: Origin https://localhost:3000 not found in Access-Control-Allow-Origin header.

SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.

Any help would be appreciated. The permission set in the manifest file is <Permissions>ReadWriteMailbox</Permissions>

outlook
outlook-addin
office-addins
outlook-restapi
asked on Stack Overflow Jul 18, 2019 by bit_junky • edited Jul 18, 2019 by bit_junky

1 Answer

0

Verify that you have CORS enabled on the server: Access-Control-Allow-Origin: *

See https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS for more details.

answered on Stack Overflow Oct 6, 2019 by Chech

User contributions licensed under CC BY-SA 3.0