curl - 如何通过curl或wget下载Google Drive URL

标签 curl google-drive-api wget

有没有办法通过curl或wget下载可公开查看的Google Drive URL?例如,能够执行以下操作:

curl -O myfile.xls https://drive.google.com/uc?export=download&id=1Wb2NfKTQr_dLoFJH0GfM0cx-t4r07IVl

请注意,我希望在可公开查看的文件上执行此操作,而无需登录我的 Google 帐户(或让其他人登录他们的帐户等)。

如果有帮助的话,我的 cors header 是:

 "kind": "drive#file",
 "id": "1Wb2NfKTQr_dLoFJH0GfM0cx-t4r07IVl",

最佳答案

这个方法怎么样?当文件太大时,Google 会返回下载该文件的代码。您可以使用代码下载该文件。当使用curl下载这么大的文件时,可以看到如下代码。

<a id="uc-download-link" class="goog-inline-block jfk-button jfk-button-action" href="/uc?export=download&amp;confirm=ABCD&amp;id=### file ID ###">download</a>

带有confirm=ABCD的查询对于下载文件很重要。该代码也包含在 cookie 中。在cookie中,可以看到如下。

#HttpOnly_.drive.google.com TRUE    /uc TRUE    #####   download_warning_#####  ABCD

在本例中,“ABCD”是代码。为了从 cookie 中检索代码并下载文件,您可以使用以下脚本。

示例脚本:

#!/bin/bash
fileid="### file id ###"
filename="MyFile.csv"
curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null
curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" -o ${filename}

如果这对您没有用,我很抱歉。

更新于 2022 年 2 月 17 日

最近,该流程的规范似乎已更改。所以我更新了这个答案。 要从 Google 云端硬盘下载公开共享的大文件,您可以使用以下脚本。

#!/bin/bash
fileid="### file id ###"
filename="MyFile.csv"
html=`curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}"`
curl -Lb ./cookie "https://drive.google.com/uc?export=download&`echo ${html}|grep -Po '(confirm=[a-zA-Z0-9\-_]+)'`&id=${fileid}" -o ${filename}
  • 在这种情况下,从 HTML 数据中检索用于下载的 ID,如下所示。

      <form id="downloadForm" action="https://drive.google.com/uc?export=download&amp;id={fileId}&amp;confirm={value for downloading}" method="post">
    
  • 当您想从 Google 云端硬盘下载公开共享的小文件时,可以使用以下命令。

      curl -L "https://drive.google.com/uc?export=download&id=### fileId ###" -o sampleoutput.csv
    

关于curl - 如何通过curl或wget下载Google Drive URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48133080/

相关文章:

WGET从网站下载pdf时出现问题

curl - 如何在 Dockerfile 中添加大的 HTTP 文件并将它们从图像层中排除?

perl - 在 Windows 上为 Perl 安装 cURL 模块

javascript - 如何在未经身份验证的情况下通过 XHR (CORS) 访问公共(public) Google Drive 文件?

google-app-engine - 将 com.google.api.services.drive.model.File 转换为 java.io.File

google-api - Google 云端硬盘 API : list files with no parent

algorithm - wget恢复算法

javascript - 如何使用 cURL 获取 JavaScript 加载的网页内容?

php - 如何使用 php-curl 发布存储在本地的二进制文件?

ssh - Wget受密码保护的整个http目录