我正在使用 Microsoft Graph API 來檢索郵件中的附件。
我使用此端點檢索附件 https://graph.microsoft.com/v1.0/users/{mailbox.mail}/messages/{message.id}/attachments/
$callAPI = @{
Headers = @{
Authorization = "Bearer $token"
}
Uri = "https://graph.microsoft.com/v1.0/users/{mailbox.mail}/messages/{message.id}/attachments/"
Method = "GET"
}
$attachments = (Invoke-RestMethod @callAPI).value;
在我使用 foreach 通過此端點檢索附件內容后:https ://graph.microsoft.com/v1.0/users/{mailbox.mail}/messages/{message.id}/attachments/{attachment.id }/ $值
$callAPI = @{
Headers = @{
Authorization = "Bearer $token"
}
Uri = "https://graph.microsoft.com/v1.0/users/{mailbox.mail}/messages/{message.id}/attachments/{attachment.id}/$value"
Method = "GET"
}
$content = Invoke-RestMethod @callAPI;
我使用這個命令列來保存檔案:
Set-Content -Path "$($attachmentsTempDestination)\$($attachment.name)" -Value $content
但是我的檔案很糟糕,Excel 無法識別。
我在 PostMan 中嘗試附件內容的端點并保存回應(圖片:PostMan 保存回應)。保存的檔案是正確的。
也許,這是我保存內容的最后一個 PowerShell 命令不好,但我不知道如何解決這個問題。
請幫我 !
通過@glen-scales 的回答,我為我的用例找到了一個很好的 PowerShell 代碼:
$callAPI = @{
Headers = @{
Authorization = "Bearer $token"
}
Uri = "https://graph.microsoft.com/v1.0/users/{mailbox.mail}/messages/{message.id}/attachments/"
Method = "GET"
}
$attachments = (Invoke-RestMethod @callAPI).value;
foreach ($attachment in $attachments) {
[System.IO.File]::WriteAllBytes("$($attachmentsTempDestination)\$($attachment.name)",[System.Convert]::FromBase64String($attachment.contentBytes))
}
uj5u.com熱心網友回復:
附件內容是 base64 編碼的字串,因此您使用 set-content 的方式只會將該 base64 內容寫入無效的檔案。
您需要首先將其轉換為位元組陣列,然后您應該能夠執行類似的操作
Set-Content -Path "$($attachmentsTempDestination)\$($attachment.name)" -Value [System.Convert]::FromBase64String($content) -Encoding Byte
否則你也可以使用(可能比 set-content 更快)
[System.IO.File]::WriteAllBytes("$($attachmentsTempDestination)\$($attachment.name)",[System.Convert]::FromBase64String($content))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/370389.html
標籤:电源外壳 接口 microsoft-graph-api 依恋
