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

使用Google Docs API读取已存在文档时遭遇404错误的问题求助

Google Docs API读取已存在文档时遭遇404错误的问题求助

我尝试用JavaScript的googleapis API读取Google Docs文件的内容,文件确实存在,请求也能正常发送,但读取内容时却收到了404错误。

复现步骤

  • 创建Google Cloud项目(遵循官方创建流程)
  • 在该项目中启用Google Docs API
  • 创建一个拥有Owner角色的服务账号
  • 编辑该服务账号,生成json格式的服务账号密钥
  • 运行以下Node.js代码,会触发404错误:
const { google } = require("googleapis")

const auth = new google.auth.GoogleAuth({
    keyFile: "<path_to_service_account_key_json_file>",
    scopes: [
        "https://www.googleapis.com/auth/documents"
    ]
})

async function getDocumentContents(documentId) {
    try {
        const authClient = await auth.getClient()
        const docs = google.docs({ version: "v1", auth: authClient })
        const response = await docs.documents.get({ documentId })
        return response.data
    } catch (error) {
        console.error(error)
    }
}

(async () => {
    const documentId = "2PACX-1vRMx5YQlZNa3ra8dYYxmv-QIQ3YJe8tbI3kqcuC7lQiZm-CSEznKfN_HYNSpoXcZIV3Y_O3YoUB1ecq"
    const documentContents = await getDocumentContents(documentId)
    console.info(documentContents.body.content)
})()

我原本期望控制台能打印出目标文档的内容,但实际却收到了404错误。我到底哪里漏了?

完整错误信息

GaxiosError: Requested entity was not found.
    at Gaxios._request (<path>/node_modules/gaxios/build/src/gaxios.js:142:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async JWT.requestAsync (<path>/node_modules/google-auth-library/build/src/auth/oauth2client.js:429:18)
    at async getDocumentContents (<path>/<file_name>.cjs:25:26)
    at async <path>/<file_name>.cjs:36:30 {
  config: {
    url: 'https://docs.googleapis.com/v1/documents/2PACX-1vRMx5YQlZNa3ra8dYYxmv-QIQ3YJe8tbI3kqcuC7lQiZm-CSEznKfN_HYNSpoXcZIV3Y_O3YoUB1ecq',
    method: 'GET',
    apiVersion: '',
    userAgentDirectives: [ [Object] ],
    paramsSerializer: [Function (anonymous)],
    headers: {
      'x-goog-api-client': 'gdcl/7.2.0 gl-node/22.11.0',
      'Accept-Encoding': 'gzip',
      'User-Agent': 'google-api-nodejs-client/7.2.0 (gzip)',
      Authorization: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.'
    },
    params: {},
    validateStatus: [Function (anonymous)],
    retry: true,
    responseType: 'unknown',
    errorRedactor: [Function: defaultErrorRedactor],
    retryConfig: {
      currentRetryAttempt: 0,
      retry: 3,
      httpMethodsToRetry: [Array],
      noResponseRetries: 2,
      retryDelayMultiplier: 2,
      timeOfFirstRequest: 1735753900182,
      totalTimeout: 9007199254740991,
      maxRetryDelay: 9007199254740991,
      statusCodesToRetry: [Array]
    }
  },
  response: {
    config: {
      url: 'https://docs.googleapis.com/v1/documents/2PACX-1vRMx5YQlZNa3ra8dYYxmv-QIQ3YJe8tbI3kqcuC7lQiZm-CSEznKfN_HYNSpoXcZIV3Y_O3YoUB1ecq',
      method: 'GET',
      apiVersion: '',
      userAgentDirectives: [Array],
      paramsSerializer: [Function (anonymous)],
      headers: [Object],
      params: {},
      validateStatus: [Function (anonymous)],
      retry: true,
      responseType: 'unknown',
      errorRedactor: [Function: defaultErrorRedactor]
    },
    data: { error: [Object] },
    headers: {
      'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
      'content-encoding': 'gzip',
      'content-type': 'application/json; charset=UTF-8',
      date: 'Wed, 01 Jan 2025 17:51:40 GMT',
      server: 'ESF',
      'transfer-encoding': 'chunked',
      vary: 'Origin, X-Origin, Referer',
      'x-content-type-options': 'nosniff',
      'x-frame-options': 'SAMEORIGIN',
      'x-l2-request-path': 'l2-managed-5',
      'x-xss-protection': '0'
    },
    status: 404,
    statusText: 'Not Found',
    request: {
      responseURL: 'https://docs.googleapis.com/v1/documents/2PACX-1vRMx5YQlZNa3ra8dYYxmv-QIQ3YJe8tbI3kqcuC7lQiZm-CSEznKfN_HYNSpoXcZIV3Y_O3YoUB1ecq'
    }
  },
  error: undefined,
  status: 404,
  code: 404,
  errors: [
    {
      message: 'Requested entity was not found.',
      domain: 'global',
      reason: 'notFound'
    }
  ],
  [Symbol(gaxios-gaxios-error)]: '6.7.1'
}

备注:内容来源于stack exchange,提问作者Giulio Meneghin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 12:04:37