none
Make calls to Outlook Rest API with Identity Token RRS feed

  • Question

  • Hey all , 

    I create some add-in for outlook in JS and I want to also make calls for outlook rest API for things I couldn't do with js API: https://dev.outlook.com/reference/add-ins/index.html 

    I see this tutorial about outlook rest API:

    https://msdn.microsoft.com/en-us/office/office365/api/mail-rest-operations#use-the-mail-rest-api

    So , I try for example just get a message , and for this I need some access token . I see this method:

    https://dev.outlook.com/reference/add-ins/Office.context.mailbox.html#getUserIdentityTokenAsync

    And I success to get some token ... but I don't know if it's enough or if it's right...

    When I send request for outlook resp API I get 2 erros:

    1. 401 unauthorized

    2. XMLHttpRequest cannot load .... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin  is therefore not allowed access. The response had HTTP status code 401.

    Code sample:

       function getIdentityToken() {
            Office.context.mailbox.getUserIdentityTokenAsync(getAsyncToken);
        }
    
        function getAsyncToken(asyncResult) { 
            var userIdentityToken = asyncResult.value;
            var xhr = new XMLHttpRequest();
        
            var ewsId = myMessageId;
            var restId = Office.context.mailbox.convertToRestId(ewsId, Office.MailboxEnums.RestVersion.v2_0);
    
            xhr.open("GET", "https://outlook.office.com/api/v2.0/emailAdress/messages/" + restId, true);
            xhr.setRequestHeader("Content-type", "application/json");
            xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
            xhr.setRequestHeader("Access-Control-Allow-Credentials", true);
            xhr.setRequestHeader("Authorization", "Bearer " + userIdentityToken);
    
            xhr.send();
            xhr.onreadystatechange = function () {
    
                if (xhr.readyState == 4) {
    
                    if (xhr.status == 200) {
                       console.log("200");
                    }
    
                }
            }
    }

    Did this way is right ? or I Must do all the levels described on link?

    Thanks all!



    Sunday, February 5, 2017 11:55 AM

All replies

  • Hi,

    This forum is for Office JS API, since your issue is related to Office 365 REST API, I would move this thread into the following forum:

    https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=Office365forDevelopers

    Thanks for your understanding.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 6, 2017 2:21 AM
    Moderator
  • Hi,

    You get these errors because the App that you are creating needs to be registered and authorized inside Azure AD. You can check the following articles for more information: 



    Regards, Sjoukje

    Please remember to click 'Mark as Answer' on the post that helps you.

    Monday, February 6, 2017 5:06 PM
    Moderator
  • Hey , Thanks . 

    I success to get right access token by the 'send box':
    https://oauthplay.azurewebsites.net/

    and then with access token I success to make request to outlook rest API .

    But,How could I do this on my add-in ?

    I register my app at : https://apps.dev.microsoft.com and set there all premissons I want ,now in my add-in I need to ask again for premonitions or I can use getAsyncToken method ??? when I try with getAsyncToken I still get same errors.

     function getAsyncToken(asyncResult) { 
            var userIdentityToken = asyncResult.value;
    }

    I try take the application ID from the portal I sign-up and set it in the manifest:

      <Id>my id ................</Id>

    But,now when I get the access token again with getAsyncToken method I still get same errors.


    I am not understand how could I get valid token in my add-in for make request to outlook rest API ?

    Thanks again !



    Tuesday, February 7, 2017 10:49 AM
  • Hi,

    The following article provides you with all the information you need on creating Outlook Add-ins. There is also sample code on how to work with AccessTokens: https://dev.office.com/docs/add-ins/outlook/use-rest-api?product=outlook


    Regards, Sjoukje

    Please remember to click 'Mark as Answer' on the post that helps you.

    Thursday, February 16, 2017 9:57 AM
    Moderator
  • Hey , it's great thanks.

    I success to make outlook rest API request from my add-in with that(for example get some message) but I have 2 problems :

    1.  I can't add attachment with outlook rest API call to message :

     https://outlook.office.com/api/v2.0/me/messages/" + itemId +"/attachments

    The error I get is:

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


    I try to set this 2 different permissions (every time another) in my manifest file but it's still same error..

      <Permissions>ReadWriteMailbox</Permissions>
      <Permissions>ReadWriteItem</Permissions>

    How could I add attachment via call to outlook rest API from my add-in ?

    2.  Sometimes the call  of getToken work and return access token and sometimes return error , I don't know also what the reason:

    Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (result){})

    Hope you could help me , thanks again you are great !




    • Edited by ADD-IN123456 Thursday, February 23, 2017 12:56 PM
    Thursday, February 23, 2017 12:53 PM
  • Hi,

    Here you can find a sample of sending a message with an attachment: https://msdn.microsoft.com/office/office365/APi/mail-rest-operations#SendMessageOnTheFly

    Can you provide the complete method for the token? Thanks!


    Regards, Sjoukje

    Please remember to click 'Mark as Answer' on the post that helps you.

    Wednesday, March 1, 2017 1:20 PM
    Moderator
  • I had same problem yet .. I post a question is stack overflow: https://stackoverflow.com/questions/42420073/cant-add-attachment-to-message-in-outlook-add-in-using-outlook-rest-api  and tell me this is a bug,but nothing change! same error {"error":{"code":"ErrorAccessDenied","message":"The api you are trying to access does not support item scoped OAuth."}}

    anyone can help ? tnx!

    Wednesday, July 5, 2017 10:23 AM