简体   繁体   English

使用 REST API v4 获取 Google 表格数据时获取“未找到请求的实体”

[英]Getting "Requested entity was not found" when fetching Google Sheets data Using REST API v4

So, I am trying to fetch data from my Google Sheets using REST API v4.因此,我正在尝试使用 REST API v4 从我的 Google 表格中获取数据。 The URL for the spreadsheet is电子表格的 URL 是

https://docs.google.com/spreadsheets/d/1OaknELJVzHxsz5UkH4asuC_xIjgVF3s7JldrxiVuRc/edit#gid=2107620388

So, my spreadsheetId is 1OaknELJVzHxsz5UkH4asuC_xIjgVF3s7JldrxiVuRc .所以,我的spreadsheetId 1OaknELJVzHxsz5UkH4asuC_xIjgVF3s7JldrxiVuRc1OaknELJVzHxsz5UkH4asuC_xIjgVF3s7JldrxiVuRc I am following this link to fetch the data.我正在关注链接以获取数据。 The API to use, as per the document is根据API要使用的API

GET https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:D5

Now, for the sheet , I have 2 sheets - transactions and categories .现在,对于工作sheet ,我有 2 张工作表 - transactionscategories 在此处输入图片说明

So, my final URL to use became所以,我要使用的最终URL变成了

https://sheets.googleapis.com/v4/spreadsheets/1OaknELJVzHxsz5UkH4asuEC_xIjgVF3s7JldrxiVuRc/values/transactions

I used the following curl call to fetch the data我使用以下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

But, I get the error as below但是,我收到如下错误

*   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

I tried other A1 Notation , but none of them worked我尝试了其他A1 Notation ,但都没有奏效

transactions!A:B
transactions!A1:B2
categories!A:B
transactions
categories

I know that my Bearer token is valid and in scope because I am able to create a spreadsheet .我知道我的不Bearer令牌有效且在范围内,因为我能够创建spreadsheet Also, I am using the following scope for my app此外,我正在为我的应用程序使用以下范围

https://www.googleapis.com/auth/drive.file

What am I doing wrong here?我在这里做错了什么? Thanks谢谢

  • You want to retrieve the values using the method of spreadsheets.values.get of Sheets API.您想使用 Sheets API 的电子表格电子表格.values.get 的方法检索值。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样?

Issue:问题:

When the error message of Requested entity was not found.Requested entity was not found.的错误消息时Requested entity was not found. occurs, the following reasons can be considered.出现这种情况,可以考虑以下原因。

  • The Spreadsheet of the spreadsheet ID is not found.未找到电子表格 ID 的电子表格。
    • If the sheet name is not found, the error message of Unable to parse range: ### is shown.如果找不到工作表名称,则会显示Unable to parse range: ###的错误消息Unable to parse range: ###
  • In the case of the scope of https://www.googleapis.com/auth/drive.file , the files by created with the access token including this scope can be accessed.https://www.googleapis.com/auth/drive.file范围的情况下,可以访问使用包含此范围的访问令牌创建的文件。 You might try to access to the Spreadsheet which is not created with the the access token including the scope.您可能会尝试访问不是使用包含范围的访问令牌创建的电子表格。

From your question, when the Spreadsheet ID is correct, it is considered that the latter is the reason of your issue.从您的问题来看,当电子表格ID正确时,则认为后者是您问题的原因。

Solution:解决方案:

In order to avoid this, I would like to propose the following 2 patterns.为了避免这种情况,我想提出以下两种模式。 Please think of this answer as just one of several answers.请将此答案视为几个答案之一。

Pattern 1:模式一:

When you are required to use the scope of https://www.googleapis.com/auth/drive.file , how about this?当你需要使用https://www.googleapis.com/auth/drive.file的范围时,这个怎么样?

  1. By the method of spreadsheets.create, create new Spreadsheet using the access token including the scope of https://www.googleapis.com/auth/drive.file .通过spreadsheets.create的方法,使用包括https://www.googleapis.com/auth/drive.file范围的访问令牌创建新的电子表格。 ANd set some values to the created Spreadsheet.并为创建的电子表格设置一些值。 This can be done by manual operation.这可以通过手动操作来完成。 And please retrieve the Spreadsheet ID.并请检索电子表格 ID。

     curl \\ -H 'Authorization: Bearer ###' \\ -H 'Content-Type: application/json' \\ -d '{"properties": {"title": "sample"}}' \\ https://sheets.googleapis.com/v4/spreadsheets
  2. Retrieve the values from the created Spreadsheet using the method of spreadsheets.values.get of Sheets API.使用Sheets API 中的spreadsheets.values.get 方法从创建的Spreadsheet 中检索值。

     curl \\ -H 'Authorization: Bearer ###' \\ https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/transactions
    • The official document says about the scope of https://www.googleapis.com/auth/drive.file as follows. 官方文档https://www.googleapis.com/auth/drive.file的作用域做了如下说明。

    View and manage Google Drive files and folders that you have opened or created with this app查看和管理您使用此应用打开或创建的 Google Drive 文件和文件夹

Pattern 2:模式2:

When you can use other scope instead of https://www.googleapis.com/auth/drive.file , how about using the scope of https://www.googleapis.com/auth/spreadsheets.readonly ?当您可以使用其他范围而不是https://www.googleapis.com/auth/drive.file ,如何使用https://www.googleapis.com/auth/spreadsheets.readonly的范围? In this case, you can retrieve the values from the existing Spreadsheet that you have the error of Requested entity was not found.在这种情况下,您可以从现有的电子表格中检索您有错误的Requested entity was not found. . .

curl \
-H 'Authorization: Bearer ###' \
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/transactions
  • In this scope, you cannot update the Spreadsheet.在此范围内,您无法更新电子表格。 If you want to update, please use https://www.googleapis.com/auth/spreadsheets .如果要更新,请使用https://www.googleapis.com/auth/spreadsheets as the scope.作为范围。 Please be careful this.请注意这一点。

Note:笔记:

  • In above sample curl commands, it supposes that you have already been able to put and get values for Spreadsheet using Sheets API.在上面的示例 curl 命令中,假设您已经能够使用 Sheets API 放置和获取电子表格的值。

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过REST API提取数据时如何处理实体关系? - How to deal with entity relations when fetching data via REST API? 使用Google Sheets REST API编辑行的单元格数据吗? - Edit cells data of a row using Google Sheets REST API? Google 电子表格 REST API v4 上的“PERMISSION_DENIED” - "PERMISSION_DENIED" on Google Spreadsheet REST API v4 使用https ssl调用rest API时找不到404 - Getting 404 not found when calling rest API using https ssl Angular V4 TypeScript Rest API - Angular v4 typescript rest api 讨价还价查找器 Max Rest API v4 - Bargain Finder Max Rest API v4 将Google表格中的选择复制到Google文档中(使用REST API?) - Copy selection from Google Sheets into a Google Doc (using REST API?) 使Paypal在上下文中使用checkout.js v4与REST API配合使用时出现问题 - Trouble getting Paypal in-context checkout.js v4 work with REST API 尝试将对象从JSP放入REST API时使用SignatureDoesNotMatch-AWS V4签名 - SignatureDoesNotMatch when trying to PUT an object to REST API from JSP - AWS V4 signature 为网站 CMS 使用 Google App 脚本/表格 / REST API - Using Google App Scripts/Sheets for a website CMS / REST API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM