day*_*mer 4 rest curl google-sheets google-drive-api google-sheets-api
因此,我尝试使用 REST API v4 从 Google 表格中获取数据。电子表格的 URL 是
https://docs.google.com/spreadsheets/d/1OaknELJVzHxsz5UkH4asuC_xIjgVF3s7JldrxiVuRc/edit#gid=2107620388
Run Code Online (Sandbox Code Playgroud)
所以,我的spreadsheetId是1OaknELJVzHxsz5UkH4asuC_xIjgVF3s7JldrxiVuRc。我正在点击此链接来获取数据。API根据文档使用的是
GET https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:D5
Run Code Online (Sandbox Code Playgroud)
现在,对于sheet,我有 2 张 -transactions和categories。

所以,我最终URL使用的变成
https://sheets.googleapis.com/v4/spreadsheets/1OaknELJVzHxsz5UkH4asuEC_xIjgVF3s7JldrxiVuRc/values/transactions
Run Code Online (Sandbox Code Playgroud)
我使用以下curl调用来获取数据
curl -v \
-H 'Authorization: Bearer ya29.GlxSB5uTisAfJ3umjYbGGdHBWeeuoBpQtytjdHc2QfP2lwVWqsQ7RwT-Or0z7VRAVQzilVWkweZ2WcH3TQqVnmoCZDe3rTn3euZ4MsoV1mCUpNh2bV-KHEhD16Q' \
-H 'Content-Type: application/json' \
https://sheets.googleapis.com/v4/spreadsheets/1OaknELJVzHxsz5UkH4asuEC_xIjgVF3s7JldrxiVuRc/values/transactions
Run Code Online (Sandbox Code Playgroud)
但是,我收到如下错误
* Trying 2607:f8b0:400a:804::200a...
* TCP_NODELAY set
* Connected to sheets.googleapis.com (2607:f8b0:400a:804::200a) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /etc/ssl/cert.pem
CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-ECDSA-CHACHA20-POLY1305
* ALPN, server accepted to use h2
* Server certificate:
* subject: C=US; ST=California; L=Mountain View; O=Google LLC; CN=*.googleapis.com
* start date: Jul 2 19:21:00 2019 GMT
* expire date: Sep 24 18:57:00 2019 GMT
* subjectAltName: host "sheets.googleapis.com" matched cert's "*.googleapis.com"
* issuer: C=US; O=Google Trust Services; CN=Google Internet Authority G3
* SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7fe5d7800000)
> GET /v4/spreadsheets/1OaknELJVzHxsUkH4asuEC_xIjgVF3s7JldrxiVuRc/values/transactions HTTP/2
> Host: sheets.googleapis.com
> User-Agent: curl/7.54.0
> Accept: */*
> Authorization: Bearer ya29.GlxSB5uTisAfJ3umjYbGGdHBWeeutytjdHc2QfP2lwVWqsQ7RwT-Or0z7VRAVQzilVWkweZ2WcH3TQqVnmoCZAehDe3rTn3euZ4MsoV1mCUpNh2bV-KHEhD16Q
> Content-Type: application/json
>
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 404
< vary: X-Origin
< vary: Referer
< vary: Origin,Accept-Encoding
< content-type: application/json; charset=UTF-8
< date: Fri, 26 Jul 2019 23:34:05 GMT
< server: ESF
< cache-control: private
< x-xss-protection: 0
< x-frame-options: SAMEORIGIN
< alt-svc: quic=":443"; ma=2592000; v="46,43,39"
< accept-ranges: none
<
{
"error": {
"code": 404,
"message": "Requested entity was not found.",
"status": "NOT_FOUND"
}
}
* Connection #0 to host sheets.googleapis.com left intact
Run Code Online (Sandbox Code Playgroud)
我尝试了其他的A1 Notation,但没有一个有效
transactions!A:B
transactions!A1:B2
categories!A:B
transactions
categories
Run Code Online (Sandbox Code Playgroud)
我知道我的Bearer令牌是有效的并且在范围内,因为我能够创建一个spreadsheet. 另外,我正在为我的应用程序使用以下范围
https://www.googleapis.com/auth/drive.file
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?谢谢
如果我的理解是正确的,这个答案怎么样?
当出现错误信息时Requested entity was not found.,可以考虑以下原因。
Unable to parse range: ###。https://www.googleapis.com/auth/drive.file,可以访问使用包含此范围的访问令牌创建的文件。您可能会尝试访问不是使用包括范围的访问令牌创建的电子表格。从您的问题来看,当电子表格ID正确时,认为后者是您问题的原因。
为了避免这种情况,我想提出以下两种模式。请将此答案视为多个答案之一。
当你需要使用 的范围时https://www.googleapis.com/auth/drive.file,这样怎么样?
通过spreadsheets.create方法,使用访问令牌创建新的Spreadsheet,其范围包括https://www.googleapis.com/auth/drive.file. 并为创建的电子表格设置一些值。这可以通过手动操作来完成。请检索电子表格 ID。
curl \
-H 'Authorization: Bearer ###' \
-H 'Content-Type: application/json' \
-d '{"properties": {"title": "sample"}}' \
https://sheets.googleapis.com/v4/spreadsheets
Run Code Online (Sandbox Code Playgroud)使用Sheets API的spreadsheets.values.get方法从创建的电子表格中检索值。
curl \
-H 'Authorization: Bearer ###' \
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/transactions
Run Code Online (Sandbox Code Playgroud)
https://www.googleapis.com/auth/drive.file如下。查看和管理您使用此应用打开或创建的 Google 云端硬盘文件和文件夹
当您可以使用其他范围代替 时https://www.googleapis.com/auth/drive.file,使用 的范围怎么样https://www.googleapis.com/auth/spreadsheets.readonly?在这种情况下,您可以从存在错误的现有电子表格中检索值Requested entity was not found.。
curl \
-H 'Authorization: Bearer ###' \
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/transactions
Run Code Online (Sandbox Code Playgroud)
https://www.googleapis.com/auth/spreadsheets. 作为范围。请小心这一点。如果我误解了你的问题并且这不是你想要的方向,我很抱歉。
| 归档时间: |
|
| 查看次数: |
11985 次 |
| 最近记录: |