将Adal JS与Office 365一起使用 - 在首次加载时使用多个Ajax调用

Tho*_*hew 7 javascript ajax oauth office365 adal

我正在使用Adal JS用O365验证我的Html应用程序.该应用程序已在Azure AD中注册.在使用devloper工具时,我注意到断点 $.ajax({多次出现(从Azure AD回调之后).这导致**的多次执行.done.我在认证中遗漏了什么吗?

$(document).ready(function () {

  (function () {
    "use strict";

    window.config = {
      subscriptionId: 'ID',
      clientId: 'ID',
      endpoints: {
        mailApi: 'https://outlook.office365.com'
      },
      cacheLocation: 'localStorage'
    };

    var authContext = new AuthenticationContext(config);

    var isCallback = authContext.isCallback(window.location.hash);

    authContext.handleWindowCallback();

    if (isCallback && !authContext.getLoginError()) {
      window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
    }

    var user = authContext.getCachedUser();
    if (!user) {
      authContext.login();
    }
    else {

      authContext.acquireToken(config.endpoints.mailApi, function (error, token) {
        if (error || !token) {
          console.log('ADAL error occurred: ' + error);
          return;
        }
        else {
          $.ajax({
            type: "GET",
            url: 'https://outlook.office365.com/api/v1.0/me/messages',
            headers: {
              'Authorization': 'Bearer ' + token,
            }
          })
            .done(function (response) {
              console.log('Successfully fetched files from OneDrive.');
              console.log(response);
            })
            .fail(function (msg, error) {
              console.log('Fetching files from OneDrive failed.');
            });
        }
      });

    }

  })();

});
Run Code Online (Sandbox Code Playgroud)