Search code examples
bashcurloauth-2.0google-drive-api

How to download a big file from google drive via curl in Bash?


I wanna make a very simple bash script for downloading files from google drive via Drive API, so in this case there is a big file on google drive and I installed OAuth 2.0 Playground on my google drive account, then in the Select the Scope box, I choose Drive API v3, and https://www.googleapis.com/auth/drive.readonly to make a token and link.

After clicking Authorize APIs and then Exchange authorization code for tokens. I copied the Access tokenlike below.

#! /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"

but it won't work, any idea ?

for example the size of my file is 12G, when I run the code I will get this as output and after a second it back to prompt again ! I checked it in two computers with two different ip addresses.(I also add alt=media to URL)

-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# 

the content of file that it created is like this

{
 "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."
 }
}

Solution

    • You want to download a file from Google Drive using the curl command with the access token.

    If my understanding is correct, how about this modification?

    Modified curl command:

    Please add the query parameter of alt=media.

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

    Note:

    • This modified curl command supposes that your access token can be used for downloading the file.
    • In this modification, the files except for Google Docs can be downloaded. If you want to download the Google Docs, please use the Files: export method of Drive API. Ref

    Reference:

    If I misunderstood your question and this was not the direction you want, I apologize.