我试图在脚本中从谷歌驱动器下载一个文件,我这样做有点麻烦。我要下载的文件在这里。

我在网上搜了很多,终于下载了其中一个。我得到了文件的uid,较小的文件(1.6MB)下载正常,但较大的文件(3.7GB)总是重定向到一个页面,询问我是否想在不进行病毒扫描的情况下继续下载。谁能帮我跳过那个屏幕?

下面是我如何让第一个文件工作-

curl -L "https://docs.google.com/uc?export=download&id=0Bz-w5tutuZIYeDU0VDRFWG9IVUE" > phlat-1.0.tar.gz

当我对另一个文件进行同样操作时,

curl -L "https://docs.google.com/uc?export=download&id=0Bz-w5tutuZIYY3h5YlMzTjhnbGM" > index4phlat.tar.gz

我得到以下输出-

我注意到在链接的第三行到最后一行,有一个&confirm=JwkK,这是一个随机的4个字符的字符串,但建议有一种方法添加到我的URL确认。我访问的一个链接建议&confirm=no_antivirus,但这不起作用。

我希望这里有人能帮忙!


当前回答

无脚本方法获得直接链接

我知道一些没有bash脚本编写经验的人正在从其他网站来到这篇文章。这是一个在浏览器中完成的解决方案。

步骤1:通常使用现有工具生成直接链接

首先,您使用所有其他现有的解决方案从您的共享链接生成一个直接链接。您可以使用https://sites.google.com/site/gdocs2direct/, https://www.wonderplugin.com/online-tools/google-drive-direct-link-generator/或https://chrome.google.com/webstore/detail/drive-direct-download/mpfdlhhpbhgghplbambikplcfpbjiail。 我将忽略这部分。

生成的直接链接如下所示:https://drive.google.com/u/0/uc?id=1Gjvcfj-8xxxxxxx8G8_jpgjcyorQ7BX5&export=download

直接链接适用于大多数小文件,但不适用于大文件。它将显示病毒警告,而不是简单地下载文件。现在我们来解决这个问题。

步骤2:修复断开的直接链接以解决病毒警告

在浏览器中打开断开的“直接”链接,您将看到“谷歌驱动器无法扫描此文件的病毒”。现在右键单击并查看页面源代码,您将看到以下文本:

<form id="downloadForm" action="https://drive.google.com/u/0/uc?id=1Gjvcfj-8xxxxxxx8G8_jpgjcyorQ7BX5&amp;export=download&amp;confirm=t&amp;uuid=5a0dd46b-521e-4ae7-8b41-0912e88b7782" method="post">

你已经找到了最后的链接!替换所有&去&并享受:

https://drive.google.com/uc?id=1Gjvcfj-8xxxxxxx8G8_jpgjcyorQ7BX5&export=download&confirm=t&uuid=c953a94e-b844-479f-8386-1ec83770fffb

大文件的其他解决方案:谷歌驱动器API

这个解决方案已经有了一个很好的答案!

其他回答

简单的方法:

(如果你只需要一次性下载)

去谷歌驱动器的网页,有下载链接 打开浏览器控制台,转到“网络”选项卡 点击下载链接 等待它的文件开始下载,并找到相应的请求(应该是列表中的最后一个),然后可以取消下载 右键单击请求并单击“复制为cURL”(或类似的)

你应该得到如下内容:

curl 'https://doc-0s-80-docs.googleusercontent.com/docs/securesc/aa51s66fhf9273i....................blah blah blah...............gEIqZ3KAQ==' --compressed

在控制台中,添加> my-file-name。扩展到最后(否则它会把文件写到你的控制台),然后按enter:)

这个链接确实有某种过期时间,所以在生成第一个请求后几分钟就不能开始下载了。

自2017年11月起生效 https://gist.github.com/ppetraki/258ea8240041e19ab258a736781f06db

#!/bin/bash

SOURCE="$1"
if [ "${SOURCE}" == "" ]; then
    echo "Must specify a source url"
    exit 1
fi

DEST="$2"
if [ "${DEST}" == "" ]; then
    echo "Must specify a destination filename"
    exit 1
fi

FILEID=$(echo $SOURCE | rev | cut -d= -f1 | rev)
COOKIES=$(mktemp)

CODE=$(wget --save-cookies $COOKIES --keep-session-cookies --no-check-certificate "https://docs.google.com/uc?export=download&id=${FILEID}" -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/Code: \1\n/p')

# cleanup the code, format is 'Code: XXXX'
CODE=$(echo $CODE | rev | cut -d: -f1 | rev | xargs)

wget --load-cookies $COOKIES "https://docs.google.com/uc?export=download&confirm=${CODE}&id=${FILEID}" -O $DEST

rm -f $COOKIES

2018年5月

如果你想使用curl从谷歌驱动器下载文件,除了驱动器中的文件id,你还需要谷歌驱动器API的OAuth2 access_token。获取令牌涉及谷歌API框架的几个步骤。谷歌的注册步骤(目前)是免费的。

OAuth2 access_token可能允许所有类型的活动,因此要小心使用它。此外,令牌会在一小段时间后超时(1小时?),但如果有人捕获它,时间还不够短,无法防止滥用。

一旦你有一个access_token和fileid,这将工作:

AUTH="Authorization: Bearer the_access_token_goes_here"
FILEID="fileid_goes_here"
URL=https://www.googleapis.com/drive/v3/files/$FILEID?alt=media
curl --header "$AUTH" $URL >myfile.ext

参见:谷歌驱动器api—REST—下载文件

我无法让Nanoix的perl脚本工作,或者我看到的其他curl示例,所以我开始自己用python研究api。这适用于小文件,但大文件阻塞了可用的ram,所以我找到了一些其他不错的分块代码,使用api的部分下载功能。要点: https://gist.github.com/csik/c4c90987224150e4a0b2

注意从API接口下载client_secret json文件到本地目录的部分。

$ cat gdrive_dl.py
from pydrive.auth import GoogleAuth  
from pydrive.drive import GoogleDrive    

"""API calls to download a very large google drive file.  The drive API only allows downloading to ram 
   (unlike, say, the Requests library's streaming option) so the files has to be partially downloaded
   and chunked.  Authentication requires a google api key, and a local download of client_secrets.json
   Thanks to Radek for the key functions: http://stackoverflow.com/questions/27617258/memoryerror-how-to-download-large-file-via-google-drive-sdk-using-python
"""

def partial(total_byte_len, part_size_limit):
    s = []
    for p in range(0, total_byte_len, part_size_limit):
        last = min(total_byte_len - 1, p + part_size_limit - 1)
        s.append([p, last])
    return s

def GD_download_file(service, file_id):
  drive_file = service.files().get(fileId=file_id).execute()
  download_url = drive_file.get('downloadUrl')
  total_size = int(drive_file.get('fileSize'))
  s = partial(total_size, 100000000) # I'm downloading BIG files, so 100M chunk size is fine for me
  title = drive_file.get('title')
  originalFilename = drive_file.get('originalFilename')
  filename = './' + originalFilename
  if download_url:
      with open(filename, 'wb') as file:
        print "Bytes downloaded: "
        for bytes in s:
          headers = {"Range" : 'bytes=%s-%s' % (bytes[0], bytes[1])}
          resp, content = service._http.request(download_url, headers=headers)
          if resp.status == 206 :
                file.write(content)
                file.flush()
          else:
            print 'An error occurred: %s' % resp
            return None
          print str(bytes[1])+"..."
      return title, filename
  else:
    return None          


gauth = GoogleAuth()
gauth.CommandLineAuth() #requires cut and paste from a browser 

FILE_ID = 'SOMEID' #FileID is the simple file hash, like 0B1NzlxZ5RpdKS0NOS0x0Ym9kR0U

drive = GoogleDrive(gauth)
service = gauth.service
#file = drive.CreateFile({'id':FILE_ID})    # Use this to get file metadata
GD_download_file(service, FILE_ID) 

警告:此功能已弃用。见下面评论中的警告。


看看这个问题:直接从谷歌驱动器使用谷歌驱动器API下载

基本上,你必须创建一个公共目录,并通过相对引用来访问你的文件

wget https://googledrive.com/host/LARGEPUBLICFOLDERID/index4phlat.tar.gz

或者,您可以使用这个脚本:https://github.com/circulosmeos/gdown.pl