You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Microsoft Graph API将垃圾邮件移至收件箱失败(返回400错误)

使用Microsoft Graph API移动垃圾邮件到收件箱返回400错误的修复方案

问题根源分析

你的代码存在三个关键错误,导致接口返回400无效请求:

  1. Authorization头格式错误:缺少Bearer前缀,Graph API要求授权头格式为Bearer {access_token}。
  2. destinationId参数错误:接口要求传入目标文件夹的唯一ID,而非显示名称(如"Inbox")。
  3. 请求体传递方式错误:设置了Content-Type: application/json,但使用data=body会以表单格式发送数据,与声明的内容类型不匹配。

修正后的代码

def move_junk_to_inbox(message_id: str):
    status, access_token = get_token()

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {access_token}"  # 补充Bearer前缀
    }
    
    base_url = "https://graph.microsoft.com/v1.0/"
    
    # 第一步:获取收件箱的唯一ID
    inbox_url = f"{base_url}users/{email_id}/mailFolders?$filter=displayName eq 'Inbox'"
    inbox_response = requests.get(inbox_url, headers=headers, timeout=TIMEOUT)
    inbox_data = inbox_response.json()
    inbox_id = inbox_data['value'][0]['id']

    # 第二步:调用move接口移动邮件
    end_point = f"users/{email_id}/messages/{message_id}/move"
    move_url = base_url + end_point

    body = {"destinationId": inbox_id}

    # 使用json参数传递请求体,自动序列化为JSON格式
    response = requests.post(move_url, headers=headers, json=body, timeout=TIMEOUT)

    return response

额外说明

  • 确保get_token()返回的access_token有效且包含Mail.ReadWrite权限(移动邮件需要该权限)。
  • 若获取收件箱ID时返回空数组,检查email_id是否正确,或调整筛选条件(部分邮箱的收件箱显示名称可能为本地化名称,如中文邮箱的"收件箱")。

内容的提问来源于stack exchange,提问作者Ravi Siswaliya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 12:35:17