使用Microsoft Graph SDK下载电子邮件附件的原始内容。

huangapple go评论96阅读模式
英文:

Download raw content of email attachment using Microsoft Graph SDK

问题

SDK中是否有一种方法允许下载电子邮件附件内容字节?

基本上,它应该根据https://learn.microsoft.com/en-us/graph/api/attachment-get?view=graph-rest-1.0&tabs=csharp#example-6-get-the-raw-contents-of-a-file-attachment-on-a-message的请求URL只需附加/$value,但我在SDK中没有看到这样的功能。

我能找到的最接近的是:

await graphClient.Users[userId].Messages[messageId].Attachments[attachmentId].GetAsync()

我期望会有类似于Attachments[attachmentId].Content.GetAsync()的方法或类似的东西。

Microsoft.Graph 5.13.0

英文:

Is there a method that allows to download the email attachment content bytes using the SDK?

Basically, it should just append /$value the request URL according to https://learn.microsoft.com/en-us/graph/api/attachment-get?view=graph-rest-1.0&tabs=csharp#example-6-get-the-raw-contents-of-a-file-attachment-on-a-message , but I don't see such feature in SDK

The closest I could get is

await graphClient.Users[userId].Messages[messageId].Attachments[attachmentId].GetAsync()

I would expect Attachments[attachmentId].Content.GetAsync() or something

Microsoft.Graph 5.13.0

答案1

得分: 2

SDK v5仍不支持下载消息附件。解决方法可以是:

  1. var requestInfo = graphClient.Users["{user_id}"]
  2. .Messages["{message_id}"]
  3. .Attachments["{attachment_id}"]
  4. .ToGetRequestInformation();
  5. requestInfo.UrlTemplate = requestInfo.UrlTemplate
  6. .Insert(requestInfo.UrlTemplate.LastIndexOf('{'), "/$value");
  7. var attachmentStream = await graphClient.RequestAdapter.SendPrimitiveAsync<Stream>(requestInfo);
英文:

It's still not supported by SDK v5 to download message's attachment. The workaround can be

  1. var requestInfo = graphClient.Users[&quot;{user_id}&quot;]
  2. .Messages[&quot;{message_id}&quot;]
  3. .Attachments[&quot;{attachment_id}&quot;]
  4. .ToGetRequestInformation();
  5. requestInfo.UrlTemplate = requestInfo.UrlTemplate
  6. .Insert(requestInfo.UrlTemplate.LastIndexOf(&#39;{&#39;), &quot;/$value&quot;);
  7. var attachmentStream = await graphClient.RequestAdapter.SendPrimitiveAsync&lt;Stream&gt;(requestInfo);

答案2

得分: 0

我无法找到通过SDK执行此操作的代码片段,所以我通过结合Graph客户端和自定义HttpClient调用编写了自己的代码。

  1. AttachmentsRequestBuilder GetAttachmentsList(string id) =>
  2. graphClient.Users[userId].Messages[id].Attachments;
  3. var attachmentList = await GetAttachmentsList(messageId)
  4. .GetAsync(cancellationToken: cancellationToken);
  5. if (attachmentList?.Value != null)
  6. {
  7. foreach (var attachmentItem in attachmentList.Value)
  8. {
  9. var attachmentRequest = GetAttachmentsList(messageId)[attachmentItem.Id];
  10. var getRequestInfo = attachmentRequest.ToGetRequestInformation();
  11. var attachmentUri = getRequestInfo.URI + "/$value";
  12. using var client = httpClientFactory.CreateClient();
  13. client.DefaultRequestHeaders.Authorization =
  14. new AuthenticationHeaderValue("Bearer", await GetToken());
  15. var response = await client.GetAsync(attachmentUri, cancellationToken);
  16. var file = await response.Content.ReadAsByteArrayAsync(cancellationToken);
  17. }
  18. }
  19. // GetToken方法
  20. private async Task<string> GetToken()
  21. {
  22. using var client = httpClientFactory.CreateClient();
  23. var @params = new Dictionary<string, string>()
  24. {
  25. { "client_id", clientId },
  26. { "client_secret", clientSecret },
  27. { "scope", "https://graph.microsoft.com/.default" },
  28. { "grant_type", "client_credentials" },
  29. };
  30. var response = await
  31. client.PostAsync($"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token",
  32. new FormUrlEncodedContent(@params));
  33. response.EnsureSuccessStatusCode();
  34. using var sr = new StreamReader(await response.Content.ReadAsStreamAsync());
  35. var body = await sr.ReadToEndAsync();
  36. var jobject = JObject.Parse(body);
  37. return jobject["access_token"]!.ToString();
  38. }

这是您提供的代码的中文翻译。

英文:

I could not find a snippet to do it via SDK, so I wrote my own by combining Graph client and custom HttpClient calls.

  1. AttachmentsRequestBuilder GetAttachmentsList(string id) =&gt;
  2. graphClient.Users[userId].Messages[id].Attachments;
  3. var attachmentList = await GetAttachmentsList(messageId)
  4. .GetAsync(cancellationToken: cancellationToken);
  5. if (attachmentList?.Value != null)
  6. {
  7. foreach (var attachmentItem in attachmentList.Value)
  8. {
  9. var attachmentRequest = GetAttachmentsList(messageId)[attachmentItem.Id];
  10. var getRequestInfo = attachmentRequest.ToGetRequestInformation();
  11. var attachmentUri = getRequestInfo.URI + &quot;/$value&quot;;
  12. using var client = httpClientFactory.CreateClient();
  13. client.DefaultRequestHeaders.Authorization =
  14. new AuthenticationHeaderValue(&quot;Bearer&quot;, await GetToken());
  15. var response = await client.GetAsync(attachmentUri, cancellationToken);
  16. var file = await response.Content.ReadAsByteArrayAsync(cancellationToken);
  17. }
  18. }

GetToken method

  1. private async Task&lt;string&gt; GetToken()
  2. {
  3. using var client = httpClientFactory.CreateClient();
  4. var @params = new Dictionary&lt;string, string&gt;()
  5. {
  6. { &quot;client_id&quot;, clientId },
  7. { &quot;client_secret&quot;, clientSecret },
  8. { &quot;scope&quot;, &quot;https://graph.microsoft.com/.default&quot; },
  9. { &quot;grant_type&quot;, &quot;client_credentials&quot; },
  10. };
  11. var response = await
  12. client.PostAsync($&quot;https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token&quot;,
  13. new FormUrlEncodedContent(@params));
  14. response.EnsureSuccessStatusCode();
  15. using var sr = new StreamReader(await response.Content.ReadAsStreamAsync());
  16. var body = await sr.ReadToEndAsync();
  17. var jobject = JObject.Parse(body);
  18. return jobject[&quot;access_token&quot;]!.ToString();
  19. }

huangapple
  • 本文由 发表于 2023年7月17日 23:19:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定