如何通过 Microsoft Graph 检索 itemAttachment 的内容 API

How to retrieve contents of an itemAttachment via the Microsoft Graph API

我目前正在开发一个通过 Microsoft Graph API 检索电子邮件的解决方案。 2015 年 11 月,Microsoft 表示它已准备好投入生产,我在另一个论坛 post 中了解到,如果您现在开始使用 Microsoft API 进行开发,您应该使用 Graph API,因为它是未来。

一切顺利,除了一件事,那就是下面的事情。

我必须检索电子邮件。在这些电子邮件中当然有附件。这些附件有一些变体。 fileAttachment(图像、文档等)、referenceAttachments 和 itemAttachments(outlook-item)。这里的问题与 itemAttachments 有关。 itemAttachment 可以是从约会到另一条消息的任何内容。这里的问题是我无法以某种适用于 fileAttachments 的方式获取和检索 contentBytes。 itemAttachment 的一个相关对象是 outlookItem。还有一个页面对此 outlookItem 进行了描述,但缺少示例和详细信息。 用户权限设置为 Mail.Read 和 Mail.Read 写入。

链接: 总体概述:http://graph.microsoft.io/docs/overview/overview 获取 outlookItem(空?):

我收到的呼叫和响应示例。请注意附件的类型。 https://graph.microsoft.com/v1.0 /用户/ /消息/ /附件

{
  "@odata.context": "link",
  "value": [
    {
      "@odata.type": "#microsoft.graph.fileAttachment",
      "id": "AAMkAGU2NmIwMTcxLTljYzUtNGRiMi1hZjczLTllNzhiZDRiNWZlZABGAAAAPAD_Lx_gimDGRqSr98J_O_e6BwDcWyYHlO7rS5_XpLHCx6NSAAIMC0V-AADcWyYHlO7rS5_XpLHCx6NSAAIMC6RgAAABEgAQAGhN_vm1RlBPt7V4N9a89UY=",
      "lastModifiedDateTime": "2016-01-13T14:25:33Z",
      "name": "image001.png",
      "contentType": "image/png",
      "size": 5077,
      "isInline": true,
      "contentId": "image001.png@01D14E16.A3A32480",
      "contentLocation": null,
      "contentBytes": "iVBORw0KGgoAAAANSUhEUgAAAKAAAACCCAIAAABOyVRHAAAAAXNSR0IArs4c6QAAEndJREFUeF7tXQ1QFFe2bkbU... (truncated)"
    },
    {
      "@odata.type": "#microsoft.graph.fileAttachment",
      "id": "AAMkAGU2NmIwMTcxLTljYzUtNGRiMi1hZjczLTllNzhiZDRiNWZlZABGAAAAPAD_Lx_gimDGRqSr98J_O_e6BwDcWyYHlO7rS5_XpLHCx6NSAAIMC0V-AADcWyYHlO7rS5_XpLHCx6NSAAIMC6RgAAABEgAQAFnSLgIC5wZOosmLtBWK8gE=",
      "lastModifiedDateTime": "2016-01-13T14:25:34Z",
      "name": "image002.png",
      "contentType": "image/png",
      "size": 3722,
      "isInline": true,
      "contentId": "image002.png@01D14E16.A3A32480",
      "contentLocation": null,
      "contentBytes": "iVBORw0KGgoAAAANSUhEUgAAAPoAAABSCAYAAAB9o8m+AAAAGXRFWHRTb... (truncated)"
    },
    {
      "@odata.type": "#microsoft.graph.fileAttachment",
      "id": "AAMkAGU2NmIwMTcxLTljYzUtNGRiMi1hZjczLTllNzhiZDRiNWZlZABGAAAAPAD_Lx_gimDGRqSr98J_O_e6BwDcWyYHlO7rS5_XpLHCx6NSAAIMC0V-AADcWyYHlO7rS5_XpLHCx6NSAAIMC6RgAAABEgAQANOuw7m8sW1Ot3MivYQ5OYU=",
      "lastModifiedDateTime": "2016-01-13T14:25:24Z",
      "name": "Knipsel.PNG",
      "contentType": null,
      "size": 7641,
      "isInline": false,
      "contentId": null,
      "contentLocation": null,
      "contentBytes": "iVBORw0KGgoAAAANSUhEUgAAAKAAAACCCAYAAADBq8MQAAA... (truncated)"
    },
    {
      "@odata.type": "#microsoft.graph.itemAttachment",
      "id": "AAMkAGU2NmIwMTcxLTljYzUtNGRiMi1hZjczLTllNzhiZDRiNWZlZABGAAAAPAD_Lx_gimDGRqSr98J_O_e6BwDcWyYHlO7rS5_XpLHCx6NSAAIMC0V-AADcWyYHlO7rS5_XpLHCx6NSAAIMC6RgAAABEgAQAPEUC740tjtAlNTe8NpopUI=",
      "lastModifiedDateTime": "2016-01-14T15:55:07Z",
      "name": "RE: Test met plaatje",
      "contentType": null,
      "size": 36972,
      "isInline": false
    }
  ]
}

我试图通过粘贴带有或不带有消息路径和扩展功能(仅支持一层深度)的附件 ID 来更改 GET 语句,但我似乎找不到解决方案. 我发现的是这个问题,有点相同,但是它是针对 office365 统一 API。 How to retrieve ItemAttachment contents from Office 365 REST API?

所以,问题是:如何通过 Microsoft Graph API 检索 outlookItem 的内容?我怎么知道会发生什么?任何人都可以帮助我克服这个障碍。

官方文档:https://graph.microsoft.io/en-us/docs/api-reference/beta/api/attachment_get.htm . Use valid Bearer authentication access code, and check for appropriate Graph API permissions on the Azure management portal. Attachment is based64 encoded string, coming in the contentBytes field. Correct Uri for loading list of a message attachments is: https://graph.microsoft.com/beta/me/messages/[邮件Id]/附件。调用附件端点的示例代码如下:

using (var client = new HttpClient())
{
    using (var request = new HttpRequestMessage(HttpMethod.Get, 
        "https://graph.microsoft.com/beta/me/messages/..id../attachments"))
    {
        request.Headers.Authorization = 
            new AuthenticationHeaderValue("Bearer", "...valid access token...");

        using (HttpResponseMessage response = await client.SendAsync(request))
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                result = await response.Content.ReadAsStringAsync();
                var json = JObject.Parse(result);
            }
        }
    }
}

使用$expand选项:

GET https://graph.microsoft.com/v1.0/me/messages('AAMkADA1M-zAAA=')/attachments('AAMkADA1M-CJKtzmnlcqVgqI=')/?$expand=microsoft.graph.itemattachment/item 

回复:

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users('d1a2fae9-db66-4cc9-8133-2184c77af1b8')/messages('AAMkADA1M-zAAA%3D')/attachments/$entity",
  "@odata.type":"#microsoft.graph.itemAttachment",
  "id":"AAMkADA1MCJKtzmnlcqVgqI=",
  "lastModifiedDateTime":"2017-07-21T00:20:34Z",
  "name":"Reminder - please bring laptop",
  "contentType":null,
  "size":32005,
  "isInline":false,
  "item@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users('d1a2fae9-db66-4cc9-8133-2184c77af1b8')/messages('AAMkADA1M-zAAA%3D')/attachments('AAMkADA1M-CJKtzmnlcqVgqI%3D')/microsoft.graph.itemAttachment/item/$entity",
  "item":{
    "@odata.type":"#microsoft.graph.message",
    "id":"",
    "createdDateTime":"2017-07-21T00:20:41Z",
    "lastModifiedDateTime":"2017-07-21T00:20:34Z",
    "receivedDateTime":"2017-07-21T00:19:55Z",
    "sentDateTime":"2017-07-21T00:19:52Z",
    "hasAttachments":false,
    "internetMessageId":"<BY2PR15MB05189A084C01F466709E414F9CA40@BY2PR15MB0518.namprd15.prod.outlook.com>",
    "subject":"Reminder - please bring laptop",
    "importance":"normal",
    "conversationId":"AAQkADA1MzMyOGI4LTlkZDctNDkzYy05M2RiLTdiN2E1NDE3MTRkOQAQAMG_NSCMBqdKrLa2EmR-lO0=",
    "isDeliveryReceiptRequested":false,
    "isReadReceiptRequested":false,
    "isRead":false,
    "isDraft":false,
    "webLink":"https://outlook.office365.com/owa/?ItemID=AAMkADA1M3MTRkOQAAAA%3D%3D&exvsurl=1&viewmodel=ReadMessageItem",
    "body":{
      "contentType":"html",
      "content":"<html><head>\r\n</head>\r\n<body>\r\n</body>\r\n</html>"
    },
    "sender":{
      "emailAddress":{
        "name":"Adele Vance",
        "address":"AdeleV@contoso.onmicrosoft.com"
      }
    },
    "from":{
      "emailAddress":{
        "name":"Adele Vance",
        "address":"AdeleV@contoso.onmicrosoft.com"
      }
    },
    "toRecipients":[
      {
        "emailAddress":{
          "name":"Alex Wilbur",
          "address":"AlexW@contoso.onmicrosoft.com"
        }
      }
    ],
    "ccRecipients":[
      {
        "emailAddress":{
          "name":"Adele Vance",
          "address":"AdeleV@contoso.onmicrosoft.com"
        }
      }
    ]
  }
}

来源:https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/attachment_get#request-2