使用Outlook API与M语言(Power Query)移动共享邮箱邮件失败求助
Let's break down the issues in your code and walk through the fixes step by step:
1. Incorrect Endpoint for Shared Mailboxes
Your current endpoint uses /me/messages/{MessageID}/move — but me refers to the authenticated user's mailbox, not the shared mailbox you're targeting. For shared mailboxes, you need to use the shared mailbox's email address or object ID in the endpoint path instead:
https://outlook.office.com/api/v2.0/users/{SharedMailboxEmail}/messages/{MessageID}/move
Replace {SharedMailboxEmail} with the full email of your shared mailbox (e.g., shared@yourdomain.com).
2. Unreplaced Placeholder Value
{MessageID} is a placeholder in your URL — you must replace this with the actual ID of the message you want to move. If you're pulling messages dynamically, you'll need to fetch the ID from your initial mailbox query and pass it into this request.
3. Missing Explicit HTTP Method
While Web.Contents switches to POST when you include a Content parameter, it's best practice to explicitly define the method to avoid any ambiguity. Add Method = "POST" to your options.
4. Authentication & Permissions
Power Query's Web.Contents relies on the authentication context you're using. Make sure:
- The account you're authenticating with has full access permissions to the shared mailbox.
- You're using an authentication method supported by the Outlook API (OAuth 2.0 is required for modern Office 365 environments). If you're getting authentication errors, check that your Power Query connection is set up to use the correct OAuth credentials.
Corrected Code Example
Here's the revised M code incorporating these fixes (replace the placeholder values with your actual data):
let SharedMailboxEmail = "shared@yourdomain.com", MessageID = "actual-message-id-here", url = "https://outlook.office.com/api/v2.0/users/" & SharedMailboxEmail & "/messages/" & MessageID & "/move", body = "{""DestinationId"": ""DeletedItems""}", Source = Json.Document( Web.Contents( url, [ Headers = [#"Content-Type"="application/json"], Method = "POST", Content = Text.ToBinary(body) ] ) ) in Source
Additional Notes
- If moving to a custom folder (not a well-known folder like DeletedItems), replace
"DeletedItems"with the folder ID of your target folder. You can fetch folder IDs using the Outlook API's/users/{SharedMailboxEmail}/mailfoldersendpoint. - Check the raw API response first (remove
Json.Documenttemporarily) — it will often include detailed error messages that can pinpoint issues like invalid IDs or permission problems.
内容的提问来源于stack exchange,提问作者Sai Madhur

