HTTP Google Sheets API v4如何在没有OAuth 2.0的情况下进行访问? [英] HTTP Google Sheets API v4 how to access without OAuth 2.0?

查看:120
本文介绍了HTTP Google Sheets API v4如何在没有OAuth 2.0的情况下进行访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的想法是创建一个Google工作表,将其公开,然后每天从我的工作计算机linux/bash访问它以读取/写入值.

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.

我有一个公开的Google文档表,任何人都可以找到/编辑. 这是工作表ID:1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0

i have a public google doc sheet that anyone can find/edit. this is the sheet ID: 1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0

按照本书 https://developers.google.com/sheets/api进行操作/samples/reading

curl 'https://sheets.googleapis.com/v4/spreadsheets/1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0/values/Sheet1!A1:A3'

返回:

{
  "error": {
  "code": 403,
  "message": "The request is missing a valid API key.",
  "status": "PERMISSION_DENIED"
  }
}

我在这里特别读了很多书 Google Sheet API v4 我发现一个复杂的解决方案.也就是说,如果您想在短短1个小时内访问您的公开表.

i've read a lot and especialy here Google Sheet API v4 i've found a complicated solution. that is if you want to access your public sheet in a short 1 hour period.

您浏览到 https://developers.google.com/oauthplayground/获得对v4 api,然后获取授权代码",然后获取刷新令牌",最后获取访问令牌".

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".

使用此访问令牌",您可以像这样访问公共表格
curl 'https://sheets.googleapis.com/v4/spreadsheets/1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0/values/Sheet1!A1:A3' -H "Authorization: Bearer ya29.GlvaBLjrTdsSuSllr3u2nAiC-BOsjvIOE1x5afU3xiafB-FTOdLWDtfabuIMGF1rId5BsZxiTXxrx7VDEtxww4Q1uvW9zRndkfm3I2LZnT1HK2nTWzX_6oXu-NAG"

using this "Access token" you can access the public sheet like this
curl 'https://sheets.googleapis.com/v4/spreadsheets/1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0/values/Sheet1!A1:A3' -H "Authorization: Bearer ya29.GlvaBLjrTdsSuSllr3u2nAiC-BOsjvIOE1x5afU3xiafB-FTOdLWDtfabuIMGF1rId5BsZxiTXxrx7VDEtxww4Q1uvW9zRndkfm3I2LZnT1HK2nTWzX_6oXu-NAG"

返回:

{
  "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?

在没有这种授权的情况下,如何使用curl访问我的Google表格表单bash? 尤其是因为我的工作表是公开的,并且任何人都可以使用浏览器进行编辑.

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.

还有其他方法可以通过其他一些永久授权来执行此操作吗? 为什么不使用电子邮件并通过?

is there another way to do this with some other permanent authorization? why not use email and pass?

提到"API密钥",但从未解释.有人可以逐步解释一下这种方法吗?

"API key" is mentioned but never explained. can some one please explain this method step by step?

推荐答案

所有Google API都要求您在[Google开发者控制台 1 并标识您自己和您的应用程序,甚至可以访问公共数据.由于您已将工作表设置为公开,因此您只需转到

All Google APIs require that you create a project on [Google developer console1 and 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 console and create a public api key remember to active the google sheets api. Then just add key=[YourKey] as a parameter on your request.

更新开发者控制台:

创建项目并获取密钥:

Google开发者控制台->创建项目->

Google developer console -> create a project -> credentials drop down -> API Key

启用:

Google开发者控制台->

Google developer console -> library find sheets enable it.

更新:

{错误":{代码":403,消息":请求缺少有效的API密钥.",状态":"PERMISSION_DENIED"}}

{ "error": { "code": 403, "message": "The request is missing a valid API key.", "status": "PERMISSION_DENIED" } }

表示您尚未向Google识别自己的身份. 2015年,Google开始要求我们确定自己的身份,您不能只使用Google API而不告诉Google您是谁.为此,您可以在[Google开发人员控制台 1 中创建一个项目.创建一个API密钥,并在所有请求中使用该API密钥.这仅适用于公共数据.

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.

https://表格. googleapis.com/v4/spreadsheets/1F6jh6756xNDlDYIvZm_3TrXb59EFEFHGEC7jdWz-Nx0/values/Sheet1!A1:A3?key=YOurKEY

注意:对于私有用户数据,您将需要使用OAuth并使用access_token =您的令牌或设置标头

Note: With private user data you would need to use OAuth and use either access_token=your token or set the header

授权:Bearer ya29.GlvaBLjrTdsSuSllr3u2nAiC-BOsjvIOE1x5afU3xiafB-FTOdLWDtfabuIMGF1rId5BsZxiTXxrx7VDEtxww4Q1uvW9zRnkfm3I2XZn-XW

Authorization: Bearer ya29.GlvaBLjrTdsSuSllr3u2nAiC-BOsjvIOE1x5afU3xiafB-FTOdLWDtfabuIMGF1rId5BsZxiTXxrx7VDEtxww4Q1uvW9zRndkfm3I2LZnT1HK2nTWzX_6oXu-NAG.

访问令牌与API密钥不同.

An access token is not the same as a API Key.

这篇关于HTTP Google Sheets API v4如何在没有OAuth 2.0的情况下进行访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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