【问题标题】:MIcrosoft API get Calendar Event description and show it appropriatelyMIcrosoft API 获取日历事件描述并适当显示
【发布时间】:2019-02-26 00:09:28
【问题描述】:

在我的 React 项目中,我调用了 Axios 来填充日历事件列表,该列表从 Microsoft Outlook 日历中获取数据(使用 Microsoft API)。结果如下

如您所见,只有事件描述给我一个问题。事实上,要显示事件描述,它会显示一个没有事件详细信息的 HTML 字符串。

我读到我必须在我的请求的标题中输入Content-type:text,但我试过了,但它不起作用。我该如何解决?这是我的 Axios 请求

getEvents(startDate, endDate, accessToken) {
    const startDateString = startDate.toISOString();
    const endDateString = endDate.toISOString();
    axios.get(
      `https://graph.microsoft.com/v1.0/users/${USER_PUBLIC_ID}/calendarview?startdatetime=${startDateString}&enddatetime=${endDateString}&orderby=start/dateTime`,
      {
        headers: {
          Authorization: `Bearer ${accessToken}`,
        },
      },
    ).then(response => this.setEvents(response.data.value))
      .catch((error) => {
        console.error(error.response);
      });
  }

【问题讨论】:

标签: reactjs microsoft-graph-api microsoft-graph-calendar


【解决方案1】:

为此,需要指定 Prefer: outlook.body-content-type="text" 标头。

According to documentation

指定要在 Body 中返回的所需格式和 GET 请求中的 UniqueBody 属性,使用 Prefer: outlook.body-content-type 标头:

  • 指定Prefer: outlook.body-content-type="text" 以获取以文本格式返回的消息正文。
  • 指定Prefer: outlook.body-content-type="html",或者只是跳过标题,以HTML格式返回邮件正文。

示例

getEvents(startDate, endDate, accessToken) {
    const startDateString = startDate.toISOString();
    const endDateString = endDate.toISOString();
    return axios.get(
      `https://graph.microsoft.com/v1.0/users/${USER_PUBLIC_ID}/calendarview?startdatetime=${startDateString}&enddatetime=${endDateString}&orderby=start/dateTime`,
      {
        headers: {
          'Authorization': `Bearer ${accessToken}`,
          'Content-Type': 'application/json',
          'Prefer' : 'outlook.body-content-type="text"'
        }
      }
    );
}

【讨论】:

  • 响应没问题,我的意思是正文内容类型是标题规范中的文本...但是 response.body.content 是一个空字符串,如果我确实不写的话标题 'Prefer' : 'outlook.body-content-type="text"',确实是一个空字符串,我有: ↵ ↵
     
  • 也许有办法阻止 Microsoft Exchange 自动转换为文本?如果是,我必须在服务器上设置一些东西,或者我也可以用代码来做一些事情?
  • 我发现问题不在于代码,而只是 Microsoft Outlook 中的权限设置,所以现在它可以工作了!
【解决方案2】:

你需要给 axios 一个配置对象。目前,您正在使用 get 属性,这就是您的代码目前不起作用的原因:

axios({
url: `https://graph.microsoft.com/v1.0/users/${USER_PUBLIC_ID}/calendarview?startdatetime=${startDateString}&enddatetime=${endDateString}&orderby=start/dateTime`,
method: "GET",
        headers: {
          Authorization: `Bearer ${accessToken}`,
          "Content-type": "text"
        },
})

您可以在这里阅读更多内容:https://github.com/axios/axios

【讨论】:

  • 我试过了,但是 JSON 响应是一样的:[![enter image description here][1]][1] [1]:i.stack.imgur.com/evBxn.png我认为问题在于body contentType是在html中,确实应该是文本。在我发表评论的链接中,一个人建议添加 > 首选:outlook.body-content-type="text" 但我不明白在我的情况下,对于 Axios,我应该如何应用该解决方案
  • 我编辑了我的答案并添加了一个自定义标题。您可以使用此语法来添加标题。只需确保拼写正确,并对带有连字符的属性使用引号。
  • 我编辑了我的文件,但它不起作用:(在响应中它仍然是“body: {contentType: "html"}"
  • 我以这种方式编辑了标题:code headers: { Authorization: Bearer ${accessToken}, Prefer: 'outlook.body-content-type="text"', }, 现在在 json 响应中我有 contentType:"text",但现在组件中的描述没有我在我的问题中发布的图像中没有字符串,但它是空的
  • 试试这样:code headers: { Authorization: Bearer ${accessToken}, Prefer: "outlook.body-content-type": "text", } 或这样:code headers: { Authorization: Bearer ${accessToken}, Prefer: outlook: { "body-content-type": "text" } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 2020-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多