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

调用Microsoft Graph API向SharePoint Excel添加行时遇参数格式错误

问题:调用Microsoft Graph API向SharePoint Excel添加行时参数格式错误

已完成认证、获取令牌及读取文件操作,在Microsoft Graph Explorer和Postman中调用添加行接口可正常执行,但本地React或Node.js环境调用时抛出错误:"The argument is invalid or missing or has an incorrect format"

请求代码(原代码)

const accessToken = await Providers.globalProvider.getAccessToken({ scopes: ['User.Read', 'Files.ReadWrite'] })

const bearer = `Bearer ${accessToken}`;

let headers = new Headers();
headers.append("Authorization", bearer);

const options = {
method: "POST,
headers: headers,
body: {
        "values": [
            [
                "column1", "column2", "column3"
            ]
        ]
    }
};
    
const addResponse = await fetch(SHAREPOINT_URL, secondOptions);

错误信息

{
    "error": {
        "code": "InvalidArgument",
        "message": "The argument is invalid or missing or has an incorrect format.",
        "innerError": {
            "code": "invalidArgument",
            "message": "The argument is invalid or missing or has an incorrect format.",
            "date": "2023-02-16T12:53:56",
            "request-id": "{request-id}",
            "client-request-id": "{client-request-id}"
        }
    }
}

解决方法

1. 修复语法与变量名错误

  • 修正method字段的引号:"POST, → "POST",(缺少闭合双引号)
  • 统一请求参数变量名:定义的是options,fetch调用时要改为options,而非secondOptions

2. 添加Content-Type请求头

必须添加application/json类型头,让服务端正确识别请求体格式:

headers.append("Content-Type", "application/json");

3. 将请求体转为JSON字符串

fetch的body不能直接传入JS对象,需通过JSON.stringify()转换为JSON字符串:

body: JSON.stringify({
    "values": [
        ["column1", "column2", "column3"]
    ]
})

修正后的完整代码

const accessToken = await Providers.globalProvider.getAccessToken({ scopes: ['User.Read', 'Files.ReadWrite'] });

const bearer = `Bearer ${accessToken}`;
let headers = new Headers();
headers.append("Authorization", bearer);
headers.append("Content-Type", "application/json"); // 新增Content-Type头

const options = {
    method: "POST", // 修复引号
    headers: headers,
    body: JSON.stringify({ // 转为JSON字符串
        "values": [
            ["column1", "column2", "column3"]
        ]
    })
};
    
const addResponse = await fetch(SHAREPOINT_URL, options); // 修正变量名

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 23:10:56