调用Outlook日历的get events api时出现No-Control-Allow-Origin问题

时间:2017-03-06 06:49:03

标签: javascript api outlook outlook-calendar

我正在尝试验证并从我的Outlook日历中获取事件。 能够登录并验证用户,但是当我试图获取事件时,我会遇到跨域问题。

这是我的代码段:

<button id="SignIn" onclick="signIn()">Sign In</button>
 <h4 id="WelcomeMessage"></h4>
  
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.12/js/adal.min.js"></script>
<script>
  var ADAL = new AuthenticationContext({
      instance: 'https://login.microsoftonline.com/',
      tenant: 'common', //COMMON OR YOUR TENANT ID

      clientId: '<its my client id>', //This is your client ID
      redirectUri: 'http://localhost:8000', //This is your redirect URI

      callback: userSignedIn,
      popUp: true
  });

	window.authContext = new AuthenticationContext(ADAL);

    var isCallback = authContext.isCallback(window.location.hash);
    authContext.handleWindowCallback();
  
  function signIn() {
      ADAL.login();
  }

  function userSignedIn(err, token) {
      console.log('userSignedIn called');
      if (!err) {
          console.log("token: " + token);
          showWelcomeMessage();
      }
      else {
          console.error("error: " + err);
      }
  }

  function showWelcomeMessage() {
      var user = ADAL.getCachedUser();
      var divWelcome = document.getElementById('WelcomeMessage');
      divWelcome.innerHTML = "Welcome " + user.profile.name;
	  loadEvents();
  }

  function loadEvents() {
  console.log("in load events");
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "https://outlook.office.com/api/v2.0/me/calendarview?startDateTime=2016-10-01T01:00:00&endDateTime=2016-10-31T23:00:00&$select=Subject", true);
  xhr.send();}
</script>

运行脚本后,这是我的错误。

this is my error

感谢任何帮助。 感谢

1 个答案:

答案 0 :(得分:0)

这是一种可能性:您的令牌对outlook.office365.com有效,但现在您正在尝试使用outlook.office.com。要检查这是否是您的情况,请检查localStorage或sessionStorage。请参阅以下图片:enter image description here