无法写入Google表格((400)Bad Request) [英] Unable to write to Google Sheets ((400) Bad Request)

查看:276
本文介绍了无法写入Google表格((400)Bad Request)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用API​​和PowerShell网络请求写入私人Google表格文档。我知道我已经正确地处理了身份验证,因为我可以使用以下代码正确地读取表单:

  $ read = Invoke -WebRequest -Urihttps://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 -Urihttps://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 :远程服务器返回错误r:(400)错误的请求。 

我做错了什么?

解决方案您的 Invoke-WebRequest 缺少 ContentType 参数,这是在你的 Method 被设置为发送数据的动词(例如 Post )。



Content-Type字段的用途是充分描述包含在主体中的数据,以便接收用户代理可以选择合适的代理或机制向用户呈现数据,或以适当的方式处理数据。

内容类型标题字段


在你的情况下,参数将是 -ContentTypeapplication / json。例如:

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

Invoke-WebRequest ContentType文档


I'm attempting to write to a private Google Sheets document using the API and PowerShell web requests. I know I have handled the authentication correctly as I can read from a sheet just fine using the following code:

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

The issue arises when I attempt to write to that same sheet (after clearing all content from the cells):

$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

I get:

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

What am I doing wrong?

解决方案

Your Invoke-WebRequest is missing the ContentType parameter, which is necessary when your Method is set to a verb for sending data (such as Post).

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

In your case, the parameter will be -ContentType "application/json". For example:

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

这篇关于无法写入Google表格((400)Bad Request)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆