Brian Pham
Brian Pham

Reputation: 561

Access to Calendar API return 401 Unauthorized

I'm new to office 365 and having problem with accessing rest api. I'm trying to test the rest api of Calendar and Mail API, so I decided to use Postman. However, to test those APIs, I need an access token in Authorization header. To figure out how to get a token, I decided to get the sample project here , configure, run and sign in on this local site to get the token cached in local storage and use that token for further requests in Postman. However, all requests I tested returned '401 unauthorized request'.

What I did:

Then I ran the app, it sign me in just fine and I can also get the token, but that token is also unauthorized to request. I decoded the token and saw the value of 'aud', it didn't return "https://outlook.office365.com/". In this url, the author said that "This should be "https://outlook.office365.com/" for the Mail, Calendar, or Contacts APIs"

So what did I miss ?

Upvotes: 0

Views: 1451

Answers (1)

Jeffrey Chen
Jeffrey Chen

Reputation: 4680

How you call the Office 365 API in AngularJS?

When signing the user in, you will only get the id_token to authenticate the user.

The aud of id_token is the tenant id (GUID).

To call the Office 365 API, you need to use AugularJS http request.

Here is a sample of sending email using Microsoft Graph API in AngularJS:

  // Build the HTTP request to send an email.
  var request = {
    method: 'POST',
    url: 'https://graph.microsoft.com/v1.0/me/microsoft.graph.sendmail',
    data: email
  };

  // Execute the HTTP request. 
  $http(request)
    .then(function (response) {
      $log.debug('HTTP request to Microsoft Graph API returned successfully.', response);     
      response.status === 202 ? vm.requestSuccess = true : vm.requestSuccess = false; 
      vm.requestFinished = true;
    }, function (error) {
      $log.error('HTTP request to Microsoft Graph API failed.');
      vm.requestSuccess= false;
      vm.requestFinished = true;
    });

Before calling the API, ADAL.js will acquire another token - access token which you can used to send the email.

enter image description here

UPDATE#1

I also downloaded the sample you mentioned. To run this sample, please ensure you have the Exchange Online > Read and writer user mail Permission assigned in your application.

enter image description here

Upvotes: 1

Related Questions