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

使用Axios下载存在的Google Docs文件时返回404错误

问题:Axios请求Google Docs导出链接返回404错误

我尝试通过Axios下载Google Docs中的文件,收到404错误。该文件确实存在且已设置共享,直接访问导出链接可正常触发下载,但使用Axios请求却失败。

错误信息

{    
    "message": "Request failed with status code 404",    
    "name": "AxiosError",    
    "stack": "AxiosError: Request failed with status code 404\n    at settle (/Users/janswist/Developer/pdf-generator/functions/node_modules/axios/dist/node/axios.cjs:1261:12)\n    at Unzip.handleStreamEnd (/Users/janswist/Developer/pdf-generator/functions/node_modules/axios/dist/node/axios.cjs:2439:11)\n    at Unzip.emit (node:events:402:35)\n    at endReadableNT (node:internal/streams/readable:1343:12)\n    at processTicksAndRejections (node:internal/process/task_queues:83:21)",    
    "config": {        
        "transitional": {            
            "silentJSONParsing": true,            
            "forcedJSONParsing": true,            
            "clarifyTimeoutError": false        
        },        
        "transformRequest": [            
            null        
        ],        
        "transformResponse": [            
            null        
        ],        
        "timeout": 0,        
        "xsrfCookieName": "XSRF-TOKEN",        
        "xsrfHeaderName": "X-XSRF-TOKEN",        
        "maxContentLength": -1,        
        "maxBodyLength": -1,        
        "env": {            
            "Blob": null        
        },        
        "headers": {            
            "Accept": "application/json, text/plain, */*",            
            "User-Agent": "axios/1.0.0",            
            "Accept-Encoding": "gzip, deflate, br"        
        },        
        "method": "get",        
        "url": "https://docs.google.com/document/d/15lhnj6sp1QFb9k-GmRCe8RVbwfbNM3es-xCt9mIu5Qc/export?format=docx",        
        "responseType": "arraybuffer"    
    },    
    "code": "ERR_BAD_REQUEST",    
    "status": 404
}

我的Axios代码

const { data } = await axios({    
    method: 'get',    
    url,    
    responseType: 'arraybuffer'  
})

解决方案

1. 模拟浏览器请求头

Google Docs导出接口会验证请求的用户代理和接受头,Axios默认头可能不被识别,修改请求头如下:

const { data } = await axios({
    method: 'get',
    url,
    responseType: 'arraybuffer',
    headers: {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
        'Referer': 'https://docs.google.com/'
    }
})

2. 确认共享权限

确保文件权限设置为「任何人有链接都可查看/下载」,如果是组织内部共享,Axios请求需携带身份验证信息(如Cookie或OAuth令牌)。

3. 启用重定向处理

Google Docs导出链接可能触发重定向,显式配置重定向参数:

const { data } = await axios({
    method: 'get',
    url,
    responseType: 'arraybuffer',
    maxRedirects: 5,
    followRedirects: true
})

4. 校验导出URL

确认URL中的文档ID和format参数(如docx、pdf)正确,直接复制浏览器中可正常下载的链接替换url变量。


内容的提问来源于stack exchange,提问作者jean d'arme

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 22:32:52