使用javascript post-request将行插入谷歌电子表格



我正试图使用浏览器控制台中的JS将新行插入到我的谷歌电子表格中使用post-man对此url 提出了一个post请求

https://sheets.googleapis.com/v4/spreadsheets/1KjrAqsNzzNIyEDb4hjx846j2AVffSeLUqi0BMR_pKPM/values/Sheet1:append?key=AIzaSyAc5XWsjlFeR3omZiYnrzaEAbZAYIWUXqI

注入这个json:

{
"asin": "aaaa",
"price": "15.99",
"title": "ratings",
"bsr" : "555523",
"image_url":"hhhhhhh"
}

我得到了这个错误:

{
"error": {
"code": 401,
"message": "API keys are not supported by this API. Expected OAuth2 access token or other authentication credentials that assert a principal. See https://cloud.google.com/docs/authentication",
"status": "UNAUTHENTICATED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "CREDENTIALS_MISSING",
"domain": "googleapis.com",
"metadata": {
"service": "sheets.googleapis.com",
"method": "google.apps.sheets.v4.SpreadsheetsService.AppendValues"
}
}
]
}
}

我的电子表格有4列:

asin    title   bsr price   image_url

您需要首先使用本文中提到的GoogleOAuth2来授权您的邮递员应用程序。

  1. 您需要在console.Cloud.Google.com和API的和服务下创建一个Google云项目>启用API和服务,查找Google Sheets API
  2. 启用API后,单击管理,然后转到凭据选项卡创建凭据
  3. 确保在授权重定向URI下;https://oauth.pstmn.io/v1/browser-callback">
  4. 此外,应用程序类型应设置为Web应用程序
  5. 下载JSON文件,并确保保存您的客户端ID和客户端机密
  6. 从下载的JSON文件中,您需要保存";auth_uri";以及";token_uri";(您将需要在您的邮递员控制台上对auth url和访问令牌url进行授权(
  7. 设置客户端ID和客户端机密;https://www.googleapis.com/auth/drive.file">
  8. 从那里,一旦您生成了访问令牌,请按照本文中的示例进行发布请求:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append.YOUR_API_KEY应该是之前在Postman控制台上生成的访问令牌

如果您在为OAuth2授权帐户时遇到其他问题,请转到谷歌云控制台上的OAuth同意屏幕,创建一个应用程序,并在非敏感范围下定义上述范围,并在测试用户下添加您的谷歌帐户。

最新更新