Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Outlook REST API Beta From an Outlook add-in

I have created an outlook add-in with ReactJS and followed this guide to get a token to be able to use the Outlook v2.0 REST APIs: https://learn.microsoft.com/en-us/office/dev/add-ins/outlook/use-rest-api

Now I would like to start using the Outlook Beta REST APIs and I figured I could use the same token to make the API calls, however I get the following error which suggests I cannot use this token:

{"error":{"code":"UnableToReadToken","message":"OAuth token submitted with the request can not be parsed.","innerError":{"requestId":"b96fc800-82d4-4b6d-8aa0-0b9ff6a36873","date":"2020-02-21T09:27:27"}}}

Is there anyway to call this API by using the token generated by Office.context.mailbox.getCallbackTokenAsync? I am aware that I can probably get an oauth2 token via Azure AD, however within the Azure AD Portal I do not have the proper admin access to follow this process so I am looking for a solution which does not rely on that.

Here is a code snippet of my functions to get the token and call the API:

getToken() {
    return new Promise(async function (resolve, reject) {
      try {
        Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (result) {
          if (result.status === "succeeded") {
            let accessToken = result.value;
            console.log(result.value);
            resolve(accessToken);
          } else {
            console.log(result.status);
            reject(result.status);
          }
        });
      } catch (error) {
        console.error(error);
        reject(error);
      }
    })
  }



getRules(token) {
    return new Promise(async function (resolve, reject) {
      try {
        const url = 'https://outlook.office.com/api/beta/me/mailfolders/inbox/messagerules';
        const header = new Headers({ 'Authorization': `Bearer ${token}` });
        const options = {
          headers: header
        };
        let response = await fetch(url, options);
        let jsonResponse = await response.json();
        console.log(jsonResponse);
        resolve(jsonResponse);
      } catch (error) {
        console.error(error);
        reject(error);
      }
    });
  }
like image 215
Riley MacDonald Avatar asked Nov 07 '22 09:11

Riley MacDonald


1 Answers

You mention not having the proper admin access to use the AD v2 authentication endpoint.

There are currently two approaches to handle app registration and user authorization. Did you confirm, if by chance one of these methods might still work...

Use Azure AD v2 authentication endpoint: https://learn.microsoft.com/en-us/previous-versions/office/office-365-api/api/beta/use-outlook-rest-api-beta#RegAuthConverged

Use Azure Active Directory and OAuth: https://learn.microsoft.com/en-us/previous-versions/office/office-365-api/api/beta/use-outlook-rest-api-beta#RegAuthAzure

...

Some additional information (which you might already be aware of):

The v2 authentication endpoint has been promoted from preview to Generally Available (GA) status for Outlook and Outlook.com developers.

If you have an in-production app that uses Windows Live API to access Outlook.com mailbox data, you must rewrite the app to use the the v2 authentication endpoint and the Outlook REST API. Because Windows Live API is being deprecated for Outlook.com, and Outlook.com users get their mailboxes enabled for the Outlook REST API, these users will get HTTP 404 errors when attempting to run such Windows Live API apps.

Read more here: https://learn.microsoft.com/en-us/previous-versions/office/office-365-api/api/beta/use-outlook-rest-api-beta

like image 103
Davanand Ganessingh Avatar answered Nov 14 '22 23:11

Davanand Ganessingh