bash - 如何通过Bash中的curl从谷歌驱动器下载大文件?

标签 bash curl oauth-2.0 google-drive-api

我想制作一个非常简单的 bash 脚本,用于通过 Drive API 从谷歌驱动器下载文件,所以在这种情况下,谷歌驱动器上有一个大文件,我安装了 OAuth 2.0 Playground在我的谷歌驱动器帐户上,然后在 Select the Scope盒子,我选择Drive API v3 , 和 https://www.googleapis.com/auth/drive.readonly制作 token 和链接。

点击后Authorize APIs然后 Exchange authorization code for tokens .我复制了 Access token像下面。

#! /bin/bash

read -p 'Enter your id : ' id
read -p 'Enter your new token : ' token
read -p 'Enter your file name : ' file

curl -H "Authorization: Bearer $token" "https://www.googleapis.com/drive/v3/files/$id?alt=media" -o "$file"

但它不起作用,知道吗?

例如我的文件的大小是 12G,当我运行代码时,我将把它作为输出,一秒钟后它又回到提示!我在两台电脑上查了两个不同的ip地址。(我还在URL中添加了alt=media)
-bash-3.2# bash mycode.sh

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   166  100   166    0     0     80      0  0:00:02  0:00:02 --:--:--    80

-bash-3.2# 

它创建的文件内容是这样的
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "downloadQuotaExceeded",
    "message": "The download quota for this file has been exceeded."
   }
  ],
  "code": 403,
  "message": "The download quota for this file has been exceeded."
 }
}

最佳答案

  • 您想使用带有访问 token 的 curl 命令从 Google Drive 下载文件。

  • 如果我的理解是正确的,这个修改怎么样?

    修改 curl 命令:

    请添加alt=media的查询参数.
    curl -H "Authorization: Bearer $token" "https://www.googleapis.com/drive/v3/files/$id?alt=media" -o "$file"
    

    笔记:
  • 此修改后的 curl 命令假设您的访问 token 可用于下载文件。
  • 在此修改中,可以下载除 Google Docs 之外的文件。如果您想下载 Google Docs,请使用 Drive API 的 Files: export 方法。 Ref

  • 引用:
  • Download files

  • 如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

    关于bash - 如何通过Bash中的curl从谷歌驱动器下载大文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60608901/

    相关文章:

    java - Spring Boot 3/Spring Security 6,忽略不安全端点的 "invalid"授权 header

    bash - 'env -i' - 不会从子 shell 中删除所有环境变量

    bash:一次从字符串中读取一个字符的文本,带空格

    bash - 如何关闭 bash 的 "readline arguments"字符重复?

    php - 与 AWS DynamoDB 通信时的整秒延迟

    azure - 连接到 Azure B2C 时如何调试 oauth2_proxy?

    linux - Cronjob 没有运行 bash 脚本

    php - 我将如何编写这个curl请求

    swift - 用 Perfect CURL Swift 解析

    facebook - 为 facebook OAuth2 指定多个重定向 URI