在SharePoint的Excel文件中添加行 (参数无效、缺失或格式不正确)。

huangapple go评论56阅读模式
英文:

Microsoft Graph add row into sharepoint excel file (The argument is invalid or missing or has an incorrect format)

问题

以下是代码部分的中文翻译:

我正在尝试通过Microsoft Graph API将一行数据添加到Excel文件中。我已经成功设置了身份验证并获取了令牌,读取了Excel文件等。

我还能够通过Microsoft的图形资源管理器工具运行请求以添加新行,它在那里完美运行,通过Postman和Microsoft Graph API添加行也运行正常。

示例请求:https://graph.microsoft.com/v1.0/me/drive/items/${ItemId}/workbook/worksheets/Sheet1/tables/${tableId}/rows/add

这在Postman和图形资源管理器中完美运行,但当我尝试在本地主机(React或Node.js)上执行此操作时,它会抛出“参数无效或缺失或格式不正确”的错误。

这是我的请求代码:

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, options);

和我收到的错误消息:

{
  "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}"
    }
  }
}
英文:

So I am trying to add a row of data into an excel file via the microsoft graph API.

I was able to successfully set up the authentication and get tokens, read the excel file, etc.

I was also able to run the request to add a new row via Microsoft's graph explorer tool and it worked there perfectly, adding the row via postman and microsoft graph api worked fine.

https://developer.microsoft.com/en-us/graph/graph-explorer

Example request: https://graph.microsoft.com/v1.0/me/drive/items/${ItemId}/workbook/worksheets/Sheet1/tables/${tableId}/rows/add

This worked perfectly from postman and the graph explorer, but when I tried to do this from my localhost (react OR nodejs) it throws "The argument is invalid or missing or has an incorrect format"

Here is my code for the request

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);

And the error message I receive

{
    "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

得分: 1

The answer was converting the javascript object body to a JSON.stringify(bodyObj). Kind of assumed it would automatically do that, but there it is!

let bodyObj = {
    values: [
        [
            "1", "2", "3"
        ]
    ]
}

let options = {
    method: "POST",
    headers: headers,
    body: JSON.stringify(bodyObj),
};
英文:

After some thought I realized the only difference between the postman POST request and my react one was the body object.

The answer was converting the javascript object body to a JSON.stringify(bodyObj). Kind of assumed it would automatically do that, but there it is!

let bodyObj = {
    values: [
        [
            "1", "2", "3"
        ]
    ]
}

let options = {
    method: "POST",
    headers: headers,
    body: JSON.stringify(bodyObj),
};

答案2

得分: 0

你的代码看起来不错,除了一个问题:你定义了 const options,但在 fetch 中传递的是 secondOptions 而不是 options

尝试使用 options

const addResponse = await fetch(SHAREPOINT_URL, options);
英文:

Your code looks good except the fact that you defined const options but in fetch you are passing secondOptions instead of options.

Try to use options

const addResponse = await fetch(SHAREPOINT_URL, options);

huangapple
  • 本文由 发表于 2023年2月16日 04:23:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定