由于不活动导致 getAttachmentContentAsync 异常,无法捕获
Exception in getAttachmentContentAsync due to inactivity, unable to catch
我正在使用 office-js 开发一个 Outlook 加载项,包含一个任务窗格,我需要在其中获取有关当前项目(电子邮件)的所有信息(基本信息、正文和附件)以将其发送到网络服务。我正在使用 Angular 8 并通过以下方式引用 API:<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
我使用函数 Office.item.body.getAsync(options, callbackFunction)
获取正文没问题,我可以使用
Office.item.getAttachmentContentAsync(item.attachments[i], options, callbackFunction)
。
当我在同一封邮件中让 Outlook Web 打开几分钟时,问题就出现了。之后,当我执行我的加载项时,对 Office.item.getAttachmentContentAsync
的调用开始失败,在控制台中写入错误:Uncaught (in promise) TypeError: Failed to fetch
。主要问题是我无法捕获异常,所以我的加载项卡住了,永远等待,因为永远不会调用 callbackFunction,添加周围的 try catch 也不起作用,因为错误似乎在一个office-js 库的内部承诺。请注意,即使在这种情况下,获取正文的调用仍然可以正常工作。
正如我使用 Chrome 工具所见,调用 Office.item.getAttachmentContentAsync
的正常行为是一个请求:
https://attachment.outlook.live.net/owa/MSA:[...]/service.svc/s/GetFileAttachment?id=[...]
返回 200 状态码。
但是当session是"expired"时,请求https://attachment.outlook.live.net/owa/MSA:[...]/service.svc/s/GetFileAttachment?id=[...]
一个302到https://outlook.live.com/owa/MSA:[...]/service.svc/s/GetAttachmentDownloadToken?redirect=[...]
那个returns一个440 Login Timeout.
我想知道有什么方法可以避免错误(检查会话是否已经过期,强制刷新会话等)或者是否有任何方法可以捕获错误(避免要卡住的加载项),以便将问题通知用户。
回调函数非常简单:
private static callbackFunction(asyncResult: Office.AsyncResult<any>){
if (asyncResult.status == Office.AsyncResultStatus.Succeeded){
[...]
}
else{
//Never called
}
}
编辑:我刚刚意识到使用 Android 的 Outlook 中的加载项,使用相同的代码无法从 Office.item.getAttachmentContentAsync
获得响应,但我确实得到了电子邮件的正文内容。在 Outlook Web 中刷新页面临时解决了这个问题,但在 Android 的 Outlook 中重新启动应用程序没有帮助。我不确定错误是否与 Android 中的 440 登录超时相同,因为我无法在那里调试加载项。
Edit2:在 Windows 的 Outlook 桌面中,我无法重现该问题,它工作正常。
感谢您报告有关 getAttachmentContentAsync 的问题。我们可以重现报告的问题。它已被放入我们的待办事项列表中。很遗憾,我们目前没有时间表可以分享
作为替代方案,我更改了我的代码以开始使用 REST API 而不是 Office.context.mailbox.item.getAttachmentContentAsync
。
此代码改编自 https://docs.microsoft.com/en-us/javascript/api/outlook/office.mailbox?view=outlook-js-preview#resturl 中的示例,即使在不支持 RequerimentSet 1.8(需要使用 getAttachmentContentAsync
)的 Android/iOS 中也始终有效。
Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (result) {
var ewsId = Office.context.mailbox.item.itemId;
var token = result.value;
var restId = Office.context.mailbox.convertToRestId(ewsId, Office.MailboxEnums.RestVersion.v2_0);
var getAttachmentsUrl = Office.context.mailbox.restUrl + '/v2.0/me/messages/' + restId + '/attachments';
var headers = { 'Authorization' : 'Bearer ' + token };
var options = { headers: headers };
myContext.httpClient.get<AttachmentsResponse>(getAttachmentsUrl, options)
.subscribe(...);
});
我正在使用 office-js 开发一个 Outlook 加载项,包含一个任务窗格,我需要在其中获取有关当前项目(电子邮件)的所有信息(基本信息、正文和附件)以将其发送到网络服务。我正在使用 Angular 8 并通过以下方式引用 API:<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
我使用函数 Office.item.body.getAsync(options, callbackFunction)
获取正文没问题,我可以使用
Office.item.getAttachmentContentAsync(item.attachments[i], options, callbackFunction)
。
当我在同一封邮件中让 Outlook Web 打开几分钟时,问题就出现了。之后,当我执行我的加载项时,对 Office.item.getAttachmentContentAsync
的调用开始失败,在控制台中写入错误:Uncaught (in promise) TypeError: Failed to fetch
。主要问题是我无法捕获异常,所以我的加载项卡住了,永远等待,因为永远不会调用 callbackFunction,添加周围的 try catch 也不起作用,因为错误似乎在一个office-js 库的内部承诺。请注意,即使在这种情况下,获取正文的调用仍然可以正常工作。
正如我使用 Chrome 工具所见,调用 Office.item.getAttachmentContentAsync
的正常行为是一个请求:
https://attachment.outlook.live.net/owa/MSA:[...]/service.svc/s/GetFileAttachment?id=[...]
返回 200 状态码。
但是当session是"expired"时,请求https://attachment.outlook.live.net/owa/MSA:[...]/service.svc/s/GetFileAttachment?id=[...]
一个302到https://outlook.live.com/owa/MSA:[...]/service.svc/s/GetAttachmentDownloadToken?redirect=[...]
那个returns一个440 Login Timeout.
我想知道有什么方法可以避免错误(检查会话是否已经过期,强制刷新会话等)或者是否有任何方法可以捕获错误(避免要卡住的加载项),以便将问题通知用户。
回调函数非常简单:
private static callbackFunction(asyncResult: Office.AsyncResult<any>){
if (asyncResult.status == Office.AsyncResultStatus.Succeeded){
[...]
}
else{
//Never called
}
}
编辑:我刚刚意识到使用 Android 的 Outlook 中的加载项,使用相同的代码无法从 Office.item.getAttachmentContentAsync
获得响应,但我确实得到了电子邮件的正文内容。在 Outlook Web 中刷新页面临时解决了这个问题,但在 Android 的 Outlook 中重新启动应用程序没有帮助。我不确定错误是否与 Android 中的 440 登录超时相同,因为我无法在那里调试加载项。
Edit2:在 Windows 的 Outlook 桌面中,我无法重现该问题,它工作正常。
感谢您报告有关 getAttachmentContentAsync 的问题。我们可以重现报告的问题。它已被放入我们的待办事项列表中。很遗憾,我们目前没有时间表可以分享
作为替代方案,我更改了我的代码以开始使用 REST API 而不是 Office.context.mailbox.item.getAttachmentContentAsync
。
此代码改编自 https://docs.microsoft.com/en-us/javascript/api/outlook/office.mailbox?view=outlook-js-preview#resturl 中的示例,即使在不支持 RequerimentSet 1.8(需要使用 getAttachmentContentAsync
)的 Android/iOS 中也始终有效。
Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (result) {
var ewsId = Office.context.mailbox.item.itemId;
var token = result.value;
var restId = Office.context.mailbox.convertToRestId(ewsId, Office.MailboxEnums.RestVersion.v2_0);
var getAttachmentsUrl = Office.context.mailbox.restUrl + '/v2.0/me/messages/' + restId + '/attachments';
var headers = { 'Authorization' : 'Bearer ' + token };
var options = { headers: headers };
myContext.httpClient.get<AttachmentsResponse>(getAttachmentsUrl, options)
.subscribe(...);
});