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

如何在本地电脑实现无需Colab的Google身份验证?

本地环境下通过Google身份验证下载Drive文件的替代方案

给你几个本地环境下的可行方案,都是通过合法的Google身份验证来下载文件,不用开放文件的「Anyone with link」权限:

方案1:用PyDrive做本地OAuth认证

PyDrive本身支持本地环境的OAuth登录,不用依赖Colab的专属模块,步骤如下:

  1. 先去Google Cloud控制台创建项目,启用Google Drive API,然后下载OAuth2客户端凭据JSON文件(操作流程:创建项目→搜索「Google Drive API」并启用→进入「凭据」页面→创建「OAuth客户端ID」→选择「桌面应用」→下载JSON文件)
  2. 安装依赖包:
    pip install pydrive oauth2client
    
  3. 运行以下代码,首次执行会弹出浏览器让你登录授权,授权后凭据会自动保存,下次运行无需重复登录:
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    
    # 初始化认证对象
    gauth = GoogleAuth()
    # 加载客户端凭据(替换成你的JSON文件路径)
    gauth.LoadCredentialsFile("client_secrets.json")
    
    if gauth.credentials is None:
        # 首次授权:弹出浏览器登录
        gauth.LocalWebserverAuth()
    elif gauth.access_token_expired:
        # 刷新过期的凭据
        gauth.Refresh()
    else:
        # 直接使用有效凭据
        gauth.Authorize()
    
    # 保存凭据到本地,方便下次使用
    gauth.SaveCredentialsFile("mycreds.txt")
    
    # 创建Drive客户端
    drive = GoogleDrive(gauth)
    
    # 下载目标文件
    file_id = 'xxxxxxxyyyyyyyyyzzzzzzzz'  # 替换成你的文件ID
    downloaded = drive.CreateFile({'id': file_id})
    downloaded.GetContentFile('weight.pt')  # 保存到本地的文件名
    

方案2:用官方Google Drive Python API

官方API稳定性更强,适合长期使用,步骤如下:

  1. 同样需要先在Google Cloud控制台创建项目、启用Drive API,并下载OAuth客户端JSON文件
  2. 安装依赖包:
    pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib
    
  3. 运行以下代码完成下载,首次执行会弹出浏览器授权:
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.http import MediaIoBaseDownload
    import io
    
    # 定义所需权限(仅需读取权限即可)
    SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
    
    def download_drive_file(file_id, output_filename):
        # 加载本地凭据
        creds = None
        try:
            # 尝试读取已保存的凭据文件
            creds = Credentials.from_authorized_user_file('token.json', SCOPES)
        except FileNotFoundError:
            # 首次授权流程
            flow = InstalledAppFlow.from_client_secrets_file(
                'client_secrets.json', SCOPES)
            creds = flow.run_local_server(port=0)
            # 保存凭据到本地
            with open('token.json', 'w') as token:
                token.write(creds.to_json())
    
        # 构建Drive服务客户端
        service = build('drive', 'v3', credentials=creds)
    
        # 发起文件下载请求
        request = service.files().get_media(fileId=file_id)
        fh = io.FileIO(output_filename, 'wb')
        downloader = MediaIoBaseDownload(fh, request)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print(f"下载进度: {int(status.progress() * 100)}%")
    
    # 调用下载函数
    file_id = 'xxxxxxxyyyyyyyyyzzzzzzzz'  # 替换成你的文件ID
    download_drive_file(file_id, 'weight.pt')
    

方案3:用rclone命令行工具(无需写代码)

如果不想写Python代码,rclone是一款强大的跨平台云存储同步工具,操作简单:

  1. 前往rclone官网下载对应操作系统的安装包并完成安装
  2. 打开终端/命令提示符,运行以下命令初始化Drive配置:
    rclone config
    
    按照提示选择「Google Drive」,然后完成OAuth授权(会自动弹出浏览器让你登录)
  3. 下载文件时,直接运行以下命令(替换成你的文件ID):
    rclone copy "drive:{xxxxxxxyyyyyyyyyzzzzzzzz}" ./weight.pt
    
    如果你知道文件在Drive中的完整路径,也可以直接用路径下载:
    rclone copy drive:模型权重/weight.pt ./
    

通用注意事项

  • 所有方案都需要先在Google Cloud控制台创建项目并启用Google Drive API,否则无法通过身份验证
  • 授权时必须使用拥有目标Drive文件访问权限的Google账号登录,否则无法下载文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 01:37:23