因此,我正在尝试使用REST API v4从我的Google表格中获取数据。电子表格的网址是
https://docs.google.com/spreadsheets/d/1OaknELJVzHxsz5UkH4asuC_xIjgVF3s7JldrxiVuRc/edit#gid=2107620388
因此,我的spreadsheetId
是1OaknELJVzHxsz5UkH4asuC_xIjgVF3s7JldrxiVuRc
。
我正在跟踪this链接以获取数据。根据文档使用的API
是
GET https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:D5
现在,对于sheet
,我有2张纸-transactions
和categories
。
因此,我最后使用的URL
变成了
https://sheets.googleapis.com/v4/spreadsheets/1OaknELJVzHxsz5UkH4asuEC_xIjgVF3s7JldrxiVuRc/values/transactions
我使用了以下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
但是,出现以下错误
* 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
我尝试了其他A1 Notation
,但没有一个起作用
transactions!A:B
transactions!A1:B2
categories!A:B
transactions
categories
我知道我的Bearer
令牌是有效的,并且在范围内,因为我能够创建一个spreadsheet
。
另外,我正在为我的应用使用以下范围
https://www.googleapis.com/auth/drive.file
我在这里做错了什么? 谢谢
答案 0 :(得分:1)
如果我的理解正确,那么这个答案如何?
出现Requested entity was not found.
错误消息时,可以考虑以下原因。
Unable to parse range: ###
。https://www.googleapis.com/auth/drive.file
范围的情况下,可以访问使用包括该范围的访问令牌创建的文件。您可能会尝试访问未使用包括范围在内的访问令牌创建的电子表格。根据您的问题,当电子表格ID正确时,就认为后者是您出问题的原因。
为了避免这种情况,我想提出以下两种模式。请认为这个答案只是几个答案之一。
当您需要使用https://www.googleapis.com/auth/drive.file
的范围时,该怎么办?
通过电子表格的方法.create,使用包含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
使用Sheets API的sheets.values.get方法从创建的电子表格中检索值。
curl \
-H 'Authorization: Bearer ###' \
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/transactions
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
https://www.googleapis.com/auth/spreadsheets
。作为范围。请注意这一点。如果我误解了您的问题,而这不是您想要的方向,我深表歉意。