我是一个相对较新的jr。系统管理员,因此如果我发出这些 API 请求的方式有任何异常,请随时告诉我。 我正在尝试从 Outlook 下载一些附件以进行一些自动统计分析。我必须从我们用于统计的特定电子邮件中获取每天都会收到的所有附件。我目前正在通过 powershell 使用 Microsoft Graph API。当我尝试下载附件时,我看到它们已被下载。然而,虽然它们的名字是正确的,但它们里面什么也没有,大小显示为0kb。 (如果重要的话,文件是.xlsx和.xml文件,总共应该下载3个)。我在论坛中搜索了与我类似的问题,但似乎没有一个有帮助。
提前谢谢您!
我在 AzureAD 中设置了应用程序,角色正确,权限设置为 Microsoft Graph Mail.Read 和 Exchange.ManageAsApp。
作为参考,这是我的 powershell 脚本,其中一些信息经过编辑。
Import-Module Microsoft.Graph.Mail
#Azure app id and secret
$clientId = "Redacted"
$clientSecret = "Redacted"
$tenantId = "Redacted"
$tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$body = @{
grant_type = "client_credentials"
client_id = $clientId
client_secret = $clientSecret
resource = "https://graph.microsoft.com"
}
$accessToken = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $body
$currentDate = Get-Date -Format "yyyy-MM-dd"
#Url for email attachments
$url = "https://graph.microsoft.com/v1.0/users/[email protected]/messages?`$filter=receivedDateTime ge $currentDate&`$orderby=receivedDateTime desc&`$top=5"
#Grab each email attachment from url
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $($accessToken.access_token)"}
# Loop through $response to download each attachment
function DownloadAttachments {
foreach ($message in $response.value) {
#Cycle attachments
$messageId = $message.id
Write-Host "Getting attachments for message: $messageId"
$attachmentsUrl = "https://graph.microsoft.com/v1.0/users/[email protected]/messages/$messageId/attachments"
$attachments = Invoke-RestMethod -Uri $attachmentsUrl -Headers @{Authorization = "Bearer $($accessToken.access_token)"}
foreach ($attachment in $attachments.value) {
#Download attachments
$attachmentId = $attachment.id
$attachmentUrl = "https://graph.microsoft.com/v1.0/users/[email protected]/messages/$messageId/attachments/$attachmentId"
$attachmentInfo = Invoke-RestMethod -Uri $attachmentUrl -Headers @{Authorization = "Bearer $($accessToken.access_token)"}
$attachmentName = $attachmentInfo.name
Write-Host "Downloading attachment: $attachmentName"
$attachmentContentUrl = $attachmentInfo['@microsoft.graph.downloadUrl']
$attachmentContent = Invoke-RestMethod -Uri $attachmentContentUrl -Headers @{Authorization = "Bearer $($accessToken.access_token)"}
$attachmentContent | Set-Content -Path "C:\Path\To\Downloads\$attachmentName" -Encoding Byte
}
}
}
DownloadAttachments
您的PowerShell脚本基本上可以正常工作,可能是您处理附件内容的方式不当。在大多数情况下,$attachmentContentUrl 通常是预先验证的 URL;因此,下载内容时不需要授权标头。
但是,当您使用 $attachmentContentUrl 参数检索附件的实际内容时,您可以选择删除 Authorization 标头。所以你可以改变这一行:
powershell
$attachmentContent = Invoke-RestMethod -Uri $attachmentContentUrl -Headers @{Authorization = "Bearer $($accessToken.access_token)"};
到此
$attachmentContent = Invoke-RestMethod -Uri $attachmentContentUrl
还需要注意的是,对于文件附件,其内容必须经过base64编码进行解码,因此,在下载此类信息时,应使用相同的过程。
例如powershell:
$attachmentContent = [System.Convert]::FromBase64String($attachmentInfo.contentBytes)
Here is a modified version of your code snippet for downloading the attachment:
powershell
Copy code
foreach ($attachment in $attachments.value) {
$attachmentId = $attachment.id
$attachmentUrl = "https://graph.microsoft.com/v1.0/users/[email protected]/messages/$messageId/attachments/$attachmentId"
$attachmentInfo = Invoke-RestMethod -Uri $attachmentUrl -Headers @{Authorization = "Bearer $($accessToken.access_token)"}.
$attachmentName = $attachmentInfo.name
Write-Host "Downloading attachment: $attachmentName"
if ($attachmentInfo.'@odata.type' -eq "#microsoft.graph.fileAttachment") {
# Assuming fileAttachment type, decode the base64 content and then save it.
$attachmentContent = [System.Convert]::FromBase64String($attachmentInfo.contentBytes)
[System.IO.File]::WriteAllBytes("C:\Path\To\Downloads\$attachmentName", $attachmentContent)
} elseif ($attachmentInfo.’@odata.type’ -eq ‘#microsoft.graph.itemAttachment’)
Item attachment may have to be treated differently.
Write-Host "Item attachment: $($attachmentInfo.name)"
} else {
Handle other attachment types if necessary.
Write-Host "Unknown attachment type: $($attachmentInfo.'@odata.type')"
}
}
如果附件是 fileAttachment 类型,它会取消对 Base64 内容进行编码,并将其保留到修改版本的文件中。根据您的需要,您可能需要以不同的方式处理 itemAttachment 类型或任何其他类型的附件。