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

使用Google Drive可下载链接下载公开文件时遇403错误求助

解决Google Drive公开文件下载403及Java下载工具适配问题

为什么复制的链接会返回403?

你从Firefox获取的doc-0c-bk-docs.googleusercontent.com开头的下载链接是临时会话绑定链接,包含nonce、uuid、hash等验证参数,这些参数和当前浏览器的会话、请求上下文(如Cookie、User-Agent)绑定,换浏览器或工具后验证不通过,就会返回403 Forbidden。

正确解决方法

1. 构造通用的公开文件下载链接

从原文件链接https://drive.google.com/file/d/1lsXIQkxqRBEbMO86OxHs8C8fET7D4oeo/view?usp=drivesdk中提取文件ID:1lsXIQkxqRBEbMO86OxHs8C8fET7D4oeo,然后构造两种通用下载链接:

  • 基础下载链接(适用于小文件):
    https://drive.google.com/uc?export=download&id=1lsXIQkxqRBEbMO86OxHs8C8fET7D4oeo
    
  • 带确认的下载链接(适用于大文件):
    如果请求基础链接后出现“无法扫描病毒”的确认页面,需要从页面源码中提取confirm参数(通常是4位字符),拼接成:
    https://drive.google.com/uc?export=download&id=FILE_ID&confirm=CONFIRM_CODE
    

2. Java下载工具的实现要点

要让工具稳定下载Google Drive文件,需要处理重定向、模拟浏览器请求头,以及大文件的确认步骤,以下是基于OkHttp的示例代码(比原生HttpURLConnection更易用):

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class GDriveDownloader {
    public static void downloadFile(String fileId, String savePath) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .followRedirects(true)
                .followSslRedirects(true)
                .build();

        // 初始请求,模拟浏览器User-Agent
        Request initialRequest = new Request.Builder()
                .url("https://drive.google.com/uc?export=download&id=" + fileId)
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36")
                .build();

        try (Response initialResponse = client.newCall(initialRequest).execute()) {
            String responseBody = initialResponse.body().string();
            // 检查是否需要确认
            int confirmIndex = responseBody.indexOf("confirm=");
            if (confirmIndex != -1) {
                // 提取4位确认码
                String confirmCode = responseBody.substring(confirmIndex + 8, confirmIndex + 12);
                Request finalRequest = new Request.Builder()
                        .url("https://drive.google.com/uc?export=download&id=" + fileId + "&confirm=" + confirmCode)
                        .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36")
                        .build();

                saveResponseStream(client.newCall(finalRequest).execute(), savePath);
            } else {
                // 小文件直接保存
                saveResponseStream(initialResponse, savePath);
            }
        }
    }

    private static void saveResponseStream(Response response, String savePath) throws IOException {
        try (InputStream inputStream = response.body().byteStream();
             FileOutputStream outputStream = new FileOutputStream(savePath)) {

            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // 替换为你的文件ID和保存路径
        downloadFile("1lsXIQkxqRBEbMO86OxHs8C8fET7D4oeo", "downloaded_file.ext");
    }
}

关键注意事项

  • 不要使用浏览器抓取的临时下载链接,这类链接仅对当前会话有效;
  • 必须设置合理的User-Agent请求头,否则Google会拒绝请求;
  • 大文件需要处理confirm参数的提取,确保请求能正确跳转到实际下载地址。

内容的提问来源于stack exchange,提问作者SoT

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 01:07:38