【问题标题】:Getting a list of attached documents and downloading them into local storage using Microsoft Graph API in UWP使用 UWP 中的 Microsoft Graph API 获取附加文档列表并将其下载到本地存储
【发布时间】:2017-06-20 17:56:31
【问题描述】:

我正在使用Microsoft Graph API 开发一个 UWP 应用。我正在使用下面的API 获取logged in 用户的当天会议列表

https://graph.microsoft.com/v1.0/me/calendarView?startDateTime=2017-06-20T20:00:00.0000000&endDateTime=2017-06-21T10:00:00.0000000 

在创建会议时,我在邀请中附加了document 以供受邀参与者使用。 收到的JSON 响应有"hasAttachments": true,。我的要求是下载邀请中发送的文件。 我需要使用我的应用程序下载这些文件,然后将它们附加并发送到participants。我该怎么做?

【问题讨论】:

  • 您尝试过什么,行为与您的预期有何不同?展示您为解决问题所做的努力(和代码)将有助于吸引答案。

标签: c# uwp microsoft-graph-api office365api


【解决方案1】:

我的要求是下载邀请中发送的文件。

您似乎正在使用List calendarView API 来获取事件。在这种情况下,您应该能够从响应中获取您想要的Event 资源的事件“id”属性,请检查"id": "string (identifier)"

之后,您可以通过List attachments API 获取本次活动的所有附件,并通过Get attachment 获取一个特殊的附件。您可以通过 contentBytes 属性获取附件的二进制内容,该属性包含文件的 base64 编码内容。如果您的附件是fileAttachment resource type。例如,如果附件是.txt 文件,您可以下载并保存在应用程序local folder 中,如下所示:

HttpClient client = new HttpClient();
string token =await AuthenticationHelper.GetTokenForUserAsync();
client.DefaultRequestHeaders.Add("Authorization", "Bearer "+token);
HttpResponseMessage httpresponse = await client.GetAsync(new Uri("https://graph.microsoft.com/v1.0/me/events/{id}/attachments/{id}"));
StorageFile downloadedfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("attachment.txt",CreationCollisionOption.ReplaceExisting); 
JObject resObj = JObject.Parse(await httpresponse.Content.ReadAsStringAsync());
string contentbyte = resObj["contentBytes"].ToString();
await FileIO.WriteTextAsync(downloadedfile, Encoding.UTF8.GetString(Convert.FromBase64String(contentbyte)));

更新: 如果附件不是.txt,实际需要的是正确地将base64-encode内容传输到文件中,例如:

StorageFile downloadedfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("attachment.xlsx", CreationCollisionOption.ReplaceExisting);    
string contentbyte = "contentbyte";
byte[] filecontent = Convert.FromBase64String(contentbyte);
await FileIO.WriteBytesAsync(downloadedfile, filecontent);

【讨论】:

  • 此代码适用于文本文档,其他文​​档如 .docx、.xls、.pdf 和 .pptx 等呢?
  • @Apoorv,你提到的这些文件格式应该可以工作。我测试了.docx.xls,它们运行良好。查看我的更新。
  • 还有一件事,我想检查用户登录时(系统时间)到当天结束时的会议。我希望将时间转换为时区并相应地显示时间。我该怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 2011-11-06
  • 2019-07-16
  • 2018-06-19
  • 2017-07-01
  • 2016-12-31
  • 1970-01-01
相关资源
最近更新 更多