在没有 Oauth 令牌的情况下访问 Google 电子表格 API

Access Google spreadsheet API without Oauth token

我已阅读 google 电子表格文档 https://developers.google.com/sheets/api/guides/authorizing 它说,如果文档是 public,则不需要 Oauth 2.0 并且 API 密钥是充足的。我正在尝试使用 hurl 和 api 键作为 parameter.it 进行测试请求,但它仍然给我错误,我需要使用 Oauth,有什么想法吗?

POST https://sheets.googleapis.com/v4/spreadsheets/16woR-nfy6KYBbkQpC2YOT1GzIean8rTzjueUnlzLMiE/values/Sheet1!A1:E1:append?valueInputOption=USER_ENTERED

回复: "error": {"code": 401,"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","status": "UNAUTHENTICATED"}

API 密钥不够。您正在尝试使用需要 OAuth 授权的 spreadsheets.values.append

Authorization Requires one of the following OAuth scopes:

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/spreadsheets
For more information, see the [Auth Guide](https://developers.google.com/identity/protocols/OAuth2).

请注意它说的是 OAuth。

在我的例子中,我需要将内容从 Google 电子表格发布到网页。我的解决方法是使用 HTTP GET 调用

GET https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range}

使用 javascript 对象 XMLHttpRequest 在此解决方案中,您只需使用 API 键

您可以使用如下格式:

https://docs.google.com/spreadsheets/d/{sheetID}/export?format=csv

将 sheetID 替换为您的 public sheet 后,与此 URL 建立 URL 连接,并按照您的意愿读取 csv 文件通常使用您选择的编程语言。

请注意,这仅适用于 Readable Public Google spreadsheets,当您不一定需要执行凭据时,这有点麻烦吉格舞