Exchange Online api 脚本出现意外的 401 错误

发布于 2025-01-09 22:32:55 字数 1828 浏览 3 评论 0 原文

我已经构建了一个脚本,可以读取我的所有公共文件夹并将电子邮件保存到本地存储。该脚本具有完全访问权限并冒充管理员用户。

首先,我进行了一次测试运行,其中注释掉了 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()
            }
        }
    }
}

I have build a script that reads out all of my public folders and saves the emails to a local storage. The script has full access rigths and does impersonate as an admin user.

At first, I did a test run where I commented out the mail.Load() row and the logic that saves the email content to the disc.

In this test run I printed out all my public folders and also all emails (Subject) and where they would be saved on my disc.

In this test run everything went fine so I did a real run. At first everything went fine but after a few hundred emails I get an error message back while trying to load the email content with mail.Load(): "the remoteserver gives back an error: 401 not autorised".

The strange thing is, that this happens in a random email in a public folder. So i can read the first ~100 emails of this folder and then this error messages pops up.

Is there some kind of protection against to many api requests that just unautorises the api so no more calls can be made for a specific time frame?

Do you guys have an idea why this 401 error is happening?

The powershell code looks somewhat like that:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绾颜 2025-01-16 22:32:55

如果您受到限制,您将不会收到 401,您将收到带有限制响应代码的 200 或 50x 错误,请参阅表 4:https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/ews-throtdling-in-exchange

如果您使用 oAuth,最可能的原因是您的令牌已过期,令牌仅有效 60 分钟,如果您使用跟踪或 fiddler 之类的工具检查获得的完整 401 响应,您应该在扩展响应中看到令牌过期错误。

如果您要处理大量项目,则代码中的其他一些内容将受到限制,因为它没有经过优化。例如,

$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(2000)

EWS 每页不会返回超过 1000 个项目,因此不要使用此选项,请将其保持在 1000 或更少。

            $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)

为什么要调用 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

$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(2000)

EWS won't return any more then a 1000 items per page so don't use this keep it to 1000 or less.

            $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)

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文