You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java通过Google API读取公开pubhtml电子表格?遇404错误求解决

解决Google Sheets API 404 "Requested entity was not found"错误

首先看你贴出的错误日志:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found { "code" : 404, "errors" : [ { "domain" : "global", "message" : "Requested entity was not found.", "reason" : "notFound" } ], "message" : "Requested entity was not found.", "status" : "NOT_FOUND" } at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) at Quickstart.main(Quickstart.java:108) Process finished with exit code 1

这个错误的核心原因很明确:你用错了ID类型。你传入的2PACX-1vTe-Cmpii1CsnufmSER7ObEuhBb4TGO29RRwcEAQjW8PYkHKQMfUvriPk2LPXyCm8HMKCIvNFXSqJkU是Google Sheets发布为网页(pubhtml)后的发布ID,而Google Sheets API需要的是表格本身的原生ID——这俩完全不是一回事,API自然找不到对应的资源,返回404。

解决步骤

1. 获取正确的Google Sheets原生ID

打开你要访问的原Google Sheet,看浏览器地址栏的URL,格式是:
https://docs.google.com/spreadsheets/d/[原生表格ID]/edit#gid=0
把中间的[原生表格ID]复制出来,它是一串短的、随机的字母数字组合(比如1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms这种),不是2PACX开头的长串。

2. 确认表格的公开权限

既然你的表格是公开的,要确保它的权限设置为**“任何人拥有查看权限”**:

  • 打开表格,点击右上角的「共享」按钮
  • 选择「知道链接的任何人」,权限设为「查看者」
  • 保存设置

3. 调整API调用代码

把你代码里的ID替换成刚拿到的原生表格ID,比如原本的调用:

Spreadsheets.Values.Get request = service.spreadsheets().values().get("2PACX-...", "Sheet1!A1:Z");

改成:

Spreadsheets.Values.Get request = service.spreadsheets().values().get("正确的原生表格ID", "Sheet1!A1:Z");

然后重新运行代码,应该就能正常获取数据了。

额外说明:如果确实要读取pubhtml内容

如果你一定要读取pubhtml格式的公开表格(而不是通过Sheets API获取结构化数据),那不需要用Google Sheets API,直接用HTTP客户端(比如Java里的HttpURLConnection或者OkHttp)请求pubhtml的URL,然后解析返回的HTML表格即可。但这种方式需要自己处理HTML解析,不如用Sheets API来得高效可靠。

内容的提问来源于stack exchange,提问作者Anoop D S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:53:30