Exchange Online api 脚本出现意外的 401 错误
我已经构建了一个脚本,可以读取我的所有公共文件夹并将电子邮件保存到本地存储。该脚本具有完全访问权限并冒充管理员用户。
首先,我进行了一次测试运行,其中注释掉了 mail.Load()
行以及将电子邮件内容保存到光盘的逻辑。
在此测试运行中,我打印了所有公共文件夹以及所有电子邮件(主题)以及它们在光盘上的保存位置。
在这次测试中一切都很顺利,所以我进行了一次真正的运行。起初一切都很顺利,但在发送了几百封电子邮件后,我在尝试使用 mail.Load()
加载电子邮件内容时收到一条错误消息:“远程服务器返回错误:401 未自动生成” 。
奇怪的是,这种情况发生在公共文件夹中的一封随机电子邮件中。所以我可以阅读该文件夹的前 100 封电子邮件,然后会弹出此错误消息。
是否有某种针对许多 api 请求的保护措施,只是取消 api 的自动化,以便在特定时间范围内不再进行调用?
你们知道为什么会出现 401 错误吗?
powershell 代码看起来有点像这样:
$folders = @()
$folders = FindAllFolders($null)
foreach($projectFolder in $folders){
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(2000)
$frFolderResult = $projectFolder.FindItems($sfCollection,$view)
$localFolder = FindLocalFolder($projectFolder)
if($localFolder -ne $null){
##Iterate Found Items
foreach ($miMailItems in $frFolderResult.Items){
$strSubject = $miMailItems.Subject
$TimeStamp = $miMailItems.DateTimeSent
$TmpMail = $localFolder.FullName + "\" + $TimeStamp.ToString("yyyyMMdd_HHmmss") + "-" + $strSubject + ".eml"
if(-Not(Test-Path $TmpMail -PathType Leaf)) {
$miMailItems.Load() #This part throws the error after a few hundred mails
$mailProps = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent)
$miMailItems.Load($mailProps)
#Write Item to Disc
$IoFile = New-Object System.IO.FileStream($TmpMail, [System.IO.FileMode]::Create)
$IoFile.Write($miMailItems.MimeContent.Content, 0, $miMailItems.MimeContent.Content.Length)
$IoFile.Close()
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您受到限制,您将不会收到 401,您将收到带有限制响应代码的 200 或 50x 错误,请参阅表 4:https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/ews-throtdling-in-exchange
如果您使用 oAuth,最可能的原因是您的令牌已过期,令牌仅有效 60 分钟,如果您使用跟踪或 fiddler 之类的工具检查获得的完整 401 响应,您应该在扩展响应中看到令牌过期错误。
如果您要处理大量项目,则代码中的其他一些内容将受到限制,因为它没有经过优化。例如,
EWS 每页不会返回超过 1000 个项目,因此不要使用此选项,请将其保持在 1000 或更少。
为什么要调用 Load Twice,这是一个单独调用的昂贵操作,而且第一次调用是多余的。通常,如果您优化代码以提高速度,您将进行批处理 https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-process-email-messages-in-batches-by-using-ews-in -交换。在您的情况下,您需要考虑首先下载的每条消息的大小,因此根据大小和数量进行批次(但将编号批次保持在 50 以下)。
You won't get a 401 if you being throttled you'll get either a 200 with a throttling response code or a 50x error see Table 4: HTTP status codes returned by throttling errors in https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/ews-throttling-in-exchange
The mostly likely cause if you using oAuth is that you token has expired, tokens are only good for 60 minutes if you examine the full 401 response your getting using either tracing or something like fiddler you should see a token expiration error in the extended response.
A few other things with you code if you are going to be processing a lot of items you will get throttled as its not optimized. eg
EWS won't return any more then a 1000 items per page so don't use this keep it to 1000 or less.
Why are you calling Load Twice this is an expensive operation to be calling on its own and the first call is redundant. Typically if your optimizing your code for speed you would be batching https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-process-email-messages-in-batches-by-using-ews-in-exchange . In your case you want to consider the size of each message your downloading first through so batch based on size and number (but keep the numbered batches under 50).