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

已获取Access Token,如何向Microsoft Graph发送带请求头的数据请求?

如何为Microsoft Graph请求传递Authorization和Accept头

你已经拿到Access Token了,接下来核心就是把请求头正确附加到API调用里——不管是GET还是POST请求,Authorization和Accept都是作为请求头字段传递的,不是URL参数或者请求体内容。下面给你几个常见工具/语言的实用示例,帮你快速搞定:

1. 使用curl命令行工具

这是最直观的测试方式,直接在终端里运行即可:

# 对应你提供的GET请求示例
curl "https://graph.microsoft.com/v1.0/me/messages?$select=subject,from,receivedDateTime&$top=25&$orderby=receivedDateTime%20DESC" \
  -H "Accept: application/json" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN_HERE"

# POST请求示例(比如创建草稿邮件)
curl "https://graph.microsoft.com/v1.0/me/messages" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN_HERE" \
  -d '{
    "subject": "Test Draft",
    "body": {
      "contentType": "Text",
      "content": "This is a test draft."
    },
    "toRecipients": [
      {
        "emailAddress": {
          "address": "recipient@example.com"
        }
      }
    ]
  }'

2. 使用JavaScript的Fetch API

前端或Node.js环境下的实现:

// GET请求获取邮件列表
async function getMessages() {
  const accessToken = "YOUR_ACCESS_TOKEN_HERE";
  const url = "https://graph.microsoft.com/v1.0/me/messages?$select=subject,from,receivedDateTime&$top=25&$orderby=receivedDateTime%20DESC";
  
  const response = await fetch(url, {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Authorization': `Bearer ${accessToken}`
    }
  });
  
  const data = await response.json();
  console.log(data);
}

// POST请求创建草稿
async function createDraft() {
  const accessToken = "YOUR_ACCESS_TOKEN_HERE";
  const url = "https://graph.microsoft.com/v1.0/me/messages";
  
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${accessToken}`
    },
    body: JSON.stringify({
      subject: "Test Draft",
      body: {
        contentType: "Text",
        content: "This is a test draft."
      },
      toRecipients: [
        {
          emailAddress: {
            address: "recipient@example.com"
          }
        }
      ]
    })
  });
  
  const data = await response.json();
  console.log(data);
}

3. 使用Python的requests库

后端开发常用的Python示例:

import requests

# GET请求获取邮件
access_token = "YOUR_ACCESS_TOKEN_HERE"
url = "https://graph.microsoft.com/v1.0/me/messages?$select=subject,from,receivedDateTime&$top=25&$orderby=receivedDateTime%20DESC"

headers = {
    'Accept': 'application/json',
    'Authorization': f'Bearer {access_token}'
}

response = requests.get(url, headers=headers)
data = response.json()
print(data)

# POST请求创建草稿
url = "https://graph.microsoft.com/v1.0/me/messages"
payload = {
    "subject": "Test Draft",
    "body": {
        "contentType": "Text",
        "content": "This is a test draft."
    },
    "toRecipients": [
        {
            "emailAddress": {
                "address": "recipient@example.com"
            }
        }
    ]
}

response = requests.post(url, json=payload, headers=headers)
data = response.json()
print(data)

解决你遇到的POST请求"bearer token empty"问题

这个错误大概率是以下几个原因导致的,你可以逐一排查:

  • Authorization头拼写错误:确保字段名是Authorization(首字母大写,无拼写错误),值的格式是Bearer (注意后面有一个空格)加上完整token,比如Bearer abc123xyz...,别漏掉空格或把Bearer写成小写。
  • 误将token放到请求体里:很多人会不小心把Authorization内容塞进POST的body,但Graph API只认请求头里的Authorization字段。
  • token无效/过期:虽然提示是empty,但如果token格式错误或已过期,也可能触发类似提示。你可以用Microsoft Graph Explorer验证token是否有效。
  • 请求头被拦截修改:前端环境下检查是否有CORS配置影响了请求头传递;后端环境下检查是否有中间件意外修改了请求头。

最后提醒:调用Graph API时,要确保你的Access Token拥有对应权限(比如读取邮件需要Mail.Read权限),否则即使token正确,也会返回权限不足的错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:32:49