javascript - 对 Outlook API 的 POST Ajax 调用 - 访问被拒绝

标签 javascript jquery ajax outlook-restapi outlook-api

我很难向 Outlook API 发送 POST 请求。

我在我的本地机器上工作并将 redirectUrl 定义到我的 localhost:port

我的 GET 请求有效:

var apiUrl = "https://outlook.office.com/api/v2.0/me/calendarview?startdatetime=" + startDateTime + "&enddatetime=" + endDateTime + "&$top=" + 10;

$.ajax({
    type: 'GET',
    url: apiUrl,
    headers: {
        "Authorization": "Bearer " + token
    }
}).done(function (data) {
    processResults(data);
}).fail(function (response) {
    handleFailure();
});

但是当我尝试发送 POST 时,我收到了这条消息:

Access is denied. Check credentials and try again.

我的代码是:

var apiUrl = "https://outlook.office.com/api/v2.0/me/events";
var postData = {
  "Subject": "Plan summer company picnic",
  "Body": {
    "ContentType": "HTML",
    "Content": "Let's kick-start this event planning!"
  },
  "Start": {
      "DateTime": "2019-01-15T11:00:00",
      "TimeZone": "Pacific Standard Time"
  },
  "End": {
      "DateTime": "2019-01-15T12:00:00",
      "TimeZone": "Pacific Standard Time"
  },
  "Attendees": [
    {
      "EmailAddress": {
        "Address": "myMail@gmail.com",
        "Name": "Name Here"
      },
      "Type": "Required"
    }
  ]
};

$.ajax({
    type: 'POST',
    url: apiUrl,
    headers: {
        "Authorization": "Bearer " + token
    },
    contentType: 'application/json',
    data: JSON.stringify(postData)
}).done(function (data) {
    processResults(data); 
}).fail(function (response) {
    handleFailure();
});

在这两种情况下,我都使用相同的 token ,所以这可能不是登录问题。我还允许在应用程序中使用 Calendars.ReadWrite

更新

我清除了缓存,登录,进行了 GET 调用,然后进行了 POST 调用,然后再次进行了 GET 调用。 GET 调用都有效,所以问题不是 token 或缓存问题。

最佳答案

问题出在身份验证过程中,显然在每个请求中我都必须指定 scope 并且它应该包括多个权限。

在标题中我发现了这个:

x-ms-diagnostics: 2000008;reason="Access to this API requires the following permissions: 'Calendars.Read.All,Calendars.Read.Shared,Calendars.ReadWrite.All,Calendars.ReadWrite.Shared'. However, the application only has the following permissions granted: 'Calendars.Read,Calendars.ReadWrite'.";error_category="invalid_grant"

即使这样也是错误的,因为 Calendars.Read.AllCalendars.ReadWrite.All 不存在,所以它应该是 Calendars.ReadCalendars.ReadWrite

身份验证请求应如下所示:

var authUrl = authServer +
          "response_type=" + encodeURI(token) +
          "&client_id=" + encodeURI(clientId) +
          "&scope=" + encodeURI("https://outlook.office.com/Calendars.Read https://outlook.office.com/Calendars.Read.Shared  https://outlook.office.com/Calendars.ReadWrite https://outlook.office.com/Calendars.ReadWrite.Shared") +
          "&redirect_uri=" + redirectUrl +
          "&state=" + stateParam;

关于javascript - 对 Outlook API 的 POST Ajax 调用 - 访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54190234/

相关文章:

javascript - 如何在数据表中过滤 AJAX 调用的输出?

javascript - jQuery Ajax 重写失败方法

javascript - 为什么我会收到意外的模板字符串表达式错误?

javascript - 如果 IE11 或 IE10,浏览器会重定向 Vuetify Vue.js 应用程序

javascript - 切换按钮状态

当我更改选项卡时,Javascript、Jquery 不起作用

javascript - 使用json的图像?

java - Wicket ajax 控件更新行为重叠

javascript - 使用ajax加载xml文件显示未定义

javascript - 在这种情况下如何使用for循环