gpt4 book ai didi

powershell - 无法写入 Google 表格((400)错误请求)

转载 作者:行者123 更新时间:2023-12-02 23:41:17 25 4
gpt4 key购买 nike

我正在尝试使用 API 和 PowerShell 网络请求写入私有(private) Google 表格文档。我知道我已经正确处理了身份验证,因为我可以使用以下代码从工作表中读取内容:

$read = Invoke-WebRequest -Uri "https://sheets.googleapis.com/v4/spreadsheets/$sheet/values/Sheet1!A1:B2?access_token=$($token.access_token)"

当我尝试写入同一张工作表(清除单元格中的所有内容后)时出现问题:

$json = @”
{
"range": "Sheet1!A1:D2",
"majorDimension": "ROWS",
"values": [
["Door", "15", "2", "3/15/2016"],
["Engine", "100", "1", "3/20/2016"]
]
}
“@

Invoke-WebRequest -Uri "https://sheets.googleapis.com/v4/spreadsheets/$sheet/values/Sheet1!A1:D2:append?valueInputOption=USER_ENTERED&access_token=$($token.access_token)" -Method Post -Body $json

我得到:

Invoke-WebRequest : The remote server returned an error: (400) Bad Request.

我做错了什么?

最佳答案

您的 Invoke-WebRequest 缺少 ContentType 参数,当您的 Method 设置为用于发送数据的动词(例如作为发布)。

The purpose of the Content-Type field is to describe the data contained in the body fully enough that the receiving user agent can pick an appropriate agent or mechanism to present the data to the user, or otherwise deal with the data in an appropriate manner.

The Content-Type Header Field

在您的情况下,参数将为 -ContentType "application/json"。例如:

Invoke-WebRequest -Uri "https://sheets.googleapis.com/v4/spreadsheets/$sheet/values/Sheet1!A1:D2:append?valueInputOption=USER_ENTERED&access_token=$($token.access_token)" -Method Post -ContentType "application/json" -Body $json 

Invoke-WebRequest ContentType Documentation

关于powershell - 无法写入 Google 表格((400)错误请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41902963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com