bash HTTP Google Sheets API v4 如何在没有 OAuth 2.0 的情况下访问?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46583052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP Google Sheets API v4 how to access without OAuth 2.0?
提问by DaImTo
my idea is to create a google sheet, make it public and then access it from my work computer linux/bash to read/write values on a daily basis.
我的想法是创建一个谷歌表,将其公开,然后从我的工作计算机 linux/bash 访问它以每天读取/写入值。
i have a public google doc sheet that anyone can find/edit.
this is the sheet ID: 1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0
我有一个公开的谷歌文档表,任何人都可以找到/编辑。这是工作表 ID:1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0
doing it by the book https://developers.google.com/sheets/api/samples/reading
按照这本书https://developers.google.com/sheets/api/samples/reading
curl 'https://sheets.googleapis.com/v4/spreadsheets/1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0/values/Sheet1!A1:A3'
curl 'https://sheets.googleapis.com/v4/spreadsheets/1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0/values/Sheet1!A1:A3'
returns:
返回:
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"status": "PERMISSION_DENIED"
}
}
i've read a lot and especialy here Google Sheet API v4i've found a complicated solution. that is if you want to access your public sheet in a short 1 hour period.
我已经阅读了很多,特别是在这里Google Sheet API v4我找到了一个复杂的解决方案。也就是说,如果您想在短短 1 小时内访问您的公共工作表。
you browse to https://developers.google.com/oauthplayground/get authorization for the v4 api, then get "Authorization code", then get "Refresh token", and finally "Access token".
您浏览到https://developers.google.com/oauthplayground/获取 v4 api 的授权,然后获取“授权代码”,然后获取“刷新令牌”,最后获取“访问令牌”。
using this "Access token" you can access the public sheet like thiscurl 'https://sheets.googleapis.com/v4/spreadsheets/1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0/values/Sheet1!A1:A3' -H "Authorization: Bearer ya29.GlvaBLjrTdsSuSllr3u2nAiC-BOsjvIOE1x5afU3xiafB-FTOdLWDtfabuIMGF1rId5BsZxiTXxrx7VDEtxww4Q1uvW9zRndkfm3I2LZnT1HK2nTWzX_6oXu-NAG"
使用此“访问令牌”,您可以像这样访问公共工作表curl 'https://sheets.googleapis.com/v4/spreadsheets/1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0/values/Sheet1!A1:A3' -H "Authorization: Bearer ya29.GlvaBLjrTdsSuSllr3u2nAiC-BOsjvIOE1x5afU3xiafB-FTOdLWDtfabuIMGF1rId5BsZxiTXxrx7VDEtxww4Q1uvW9zRndkfm3I2LZnT1HK2nTWzX_6oXu-NAG"
returns:
返回:
{
"range": "Sheet1!A1:A3",
"majorDimension": "ROWS",
"values": [
[
"a1"
],
[
"a2"
],
[
"a3"
]
]
}
perfect. in theory the "Access token" expires after an hour, the "Refresh token" never expires. so you would save the tokens, try to read the sheet with the "Access token", if it fails use the "Refresh token" to gain a new "Access token" and carry on.
完美的。理论上,“访问令牌”在一小时后过期,“刷新令牌”永不过期。因此,您将保存令牌,尝试使用“访问令牌”读取工作表,如果失败,请使用“刷新令牌”获取新的“访问令牌”并继续。
but, i've had a dozen of "Refresh token"s that were redeemed/expired, "Authorization code"s expired, all in all nothing works after a few hours. why?
但是,我有十几个“刷新令牌”已被赎回/过期,“授权码”已过期,总而言之,几个小时后没有任何效果。为什么?
how can i access my google sheet form bash with curl without this kind of authorization? especially since my sheet is public and can be edited by anyone with a browser.
没有这种授权,我如何使用 curl 访问我的谷歌表单 bash?特别是因为我的工作表是公开的,任何人都可以使用浏览器进行编辑。
is there another way to do this with some other permanent authorization? why not use email and pass?
有没有另一种方法可以通过其他永久授权来做到这一点?为什么不使用电子邮件和通过?
"API key" is mentioned but never explained. can some one please explain this method step by step?
提到了“API 密钥”,但从未解释过。有人可以一步一步解释这个方法吗?
回答by DaImTo
All Google APIs require that you create a project on [Google developer console1and identify yourself and your application, even to access public data. Since you have set the sheet to public you can just go to google developer consoleand create a public api key remember to active the google sheets api. Then just add key=[YourKey] as a parameter on your request.
所有 Google API 都要求您在 [Google 开发者控制台1上创建项目并标识您自己和您的应用程序,甚至访问公共数据。由于您已将工作表设置为公开,您只需转到谷歌开发者控制台并创建一个公共 api 密钥,请记住激活谷歌工作表 api。然后只需在您的请求中添加 key=[YourKey] 作为参数。
Update Dev console:
更新开发控制台:
Create project and get key:
创建项目并获取密钥:
Google developer console-> create a project -> credentialsdrop down -> API Key
Google 开发者控制台-> 创建项目 ->凭据下拉列表 -> API 密钥
Enable it:
启用它:
Google developer console-> libraryfind sheets enable it.
Google 开发者控制台->库查找表启用它。
Update:
更新:
{ "error": { "code": 403, "message": "The request is missing a valid API key.", "status": "PERMISSION_DENIED" } }
{ "error": { "code": 403, "message": "请求缺少有效的 API 密钥。", "status": "PERMISSION_DENIED" } }
Means that you have not identified yourself to Google. In 2015 Google Start to require that we identify ourselves you cant just use a Google API without telling google who you are. You do that by creating a project on [Google developer console1. Create an API key and use that API key in all of your requests. This only works with Public data.
表示您尚未向 Google 表明自己的身份。2015 年,Google 开始要求我们证明自己的身份,您不能只使用 Google API 而不告诉 google 您是谁。您可以通过在 [Google 开发者控制台1. 创建一个 API 密钥并在您的所有请求中使用该 API 密钥。这仅适用于公共数据。
Note: With private user data you would need to use OAuth and use either access_token=your token or set the header
注意:对于私人用户数据,您需要使用 OAuth 并使用 access_token=your token 或设置标头
Authorization: Bearer ya29.GlvaBLjrTdsSuSllr3u2nAiC-BOsjvIOE1x5afU3xiafB-FTOdLWDtfabuIMGF1rId5BsZxiTXxrx7VDEtxww4Q1uvW9zRndkfm3I2LZnT1HK2nTWzX_6oXu-NAG.
授权:承载 ya29.GlvaBLjrTdsSuSllr3u2nAiC-BOsjvIOE1x5afU3xiafB-FTOdLWDtfabuIMGF1rId5BsZxiTXxrx7VDEtxww4Q1uvW9zRndkfm3I2LZnTzXhk2GnT
An access token is not the same as a API Key.
访问令牌与 API 密钥不同。
回答by Tom Larkworthy
Another approach is to share the sheet with a Google Cloud service account, and then use the service account to authorize with the API client. If the document is public you do not need to share it.
另一种方法是与 Google Cloud 服务帐户共享工作表,然后使用服务帐户向 API 客户端授权。如果文档是公开的,则无需共享。
Instructions on creating a service account and reading a Google doc, which should also apply to sheets https://www.futurice.com/blog/read-goog-doc-using-service-account
关于创建服务帐户和阅读 Google 文档的说明,这也适用于工作表https://www.futurice.com/blog/read-goog-doc-using-service-account