How to download a Google Drive url via curl or wget

Is there a way to download a publicly-viewable Google Drive url via curl or wget? For example, being able to do something like:

curl -O myfile.xls https://drive.google.com/uc?export=download&id=1Wb2NfKTQr_dLoFJH0GfM0cx-t4r07IVl

Note, I'm looking to do this on a publicly-viewable file without having to sign into my Google account (or have someone else sign into their account, etc.).

If helpful, the cors headers I have are:

 "kind": "drive#file",
 "id": "1Wb2NfKTQr_dLoFJH0GfM0cx-t4r07IVl",

Solution 1:

How about this method? When the file is such large size, Google returns a code for downloading the file. You can download the file using the code. When such large file is downloaded using curl, you can see the code as follows.

<a id="uc-download-link" class="goog-inline-block jfk-button jfk-button-action" href="/uc?export=download&amp;confirm=ABCD&amp;id=### file ID ###">download</a>

The query with confirm=ABCD is important for downloading the file. This code is also included in the cookie. At the cookie, you can see it as follows.

#HttpOnly_.drive.google.com TRUE    /uc TRUE    #####   download_warning_#####  ABCD

In this case, "ABCD" is the code. In order to retrieve the code from the cookie and download the file, you can use the following script.

Sample script :

#!/bin/bash
fileid="### file id ###"
filename="MyFile.csv"
curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null
curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" -o ${filename}

If this was not useful for you, I'm sorry.

Solution 2:

Simplest and best way (with a real Google Drive file example)

  1. Install gdown using pip

    • Command - pip install gdown
  2. Let's say I wish to download cnn_stories.tgz from Google Drive

    • Download Link: https://drive.google.com/uc?export=download&id=0BwmD_VLjROrfTHk4NFg2SndKcjQ
  3. Please note the id URL parameter 0BwmD_VLjROrfTHk4NFg2SndKcjQ in the link

  4. That's it! Download the file using gdown

    • gdown --id 0BwmD_VLjROrfTHk4NFg2SndKcjQ --output cnn_stories.tgz


TLDR: gdown --id {gdrive_file_id} --output {file_name}


Command Line Args:

--id : Google drive file ID

--output: Output File name