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

前端 未结 2 972
轮回少年
轮回少年 2020-12-11 05:02

I\'m currently developing a solution which is retrieving e-mails via the Microsoft Graph API. In november 2015 Microsoft stated it is ready for production and I\'ve read in

相关标签:
2条回答
  • 2020-12-11 05:30

    Use $expand option:

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

    Response:

    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"
            }
          }
        ]
      }
    }
    

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

    0 讨论(0)
  • 2020-12-11 05:34

    The official documentation: 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/[ message Id ]/attachments. Sample code to call attachments endpoint is below:

    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);
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题