下载整个Google云端硬盘公共共享文件夹,无需进行任何身份验证

时间:2019-05-10 00:57:10

标签: python google-drive-api google-colaboratory pydrive

我正在寻找一种从公开共享的Google Drive文件夹链接下载整个Google Drive文件夹的方法,而不必提供我的Google凭据。

这个SO问题显示了如何下载公共共享的Google Drive文件夹的全部内容,但是它要求Pydrive访问您的Google凭据。

Python: How do download entire folder from Google Drive

此代码无需下载Google凭据即可下载单个公共共享的Google驱动器文件。

import os  
import requests

def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)    

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value

    return None

def save_response_content(response, destination):
    CHUNK_SIZE = 32768

    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

file_id = '1thX75_cMkly5btgxe4dgV6YDKnrUczZs' #google drive file id

download_file_from_google_drive(file_id, 'thefile.extension')

是否可以在不提供Google凭据的情况下下载公共共享的Google Drive文件夹的全部内容?

0 个答案:

没有答案
相关问题