curlgoogle-drive-apiwget

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

  • 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.


    Updated at February 17, 2022

    Recently, it seems that the specification of this flow has been changed. So I updated this answer. In order to download a publicly shared file of large size from Google Drive, you can use the following script.

    #!/bin/bash
    fileid="### file id ###"
    filename="MyFile.csv"
    html=`curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}"`
    curl -Lb ./cookie "https://drive.google.com/uc?export=download&`echo ${html}|grep -Po '(confirm=[a-zA-Z0-9\-_]+)'`&id=${fileid}" -o ${filename}
    

    Updated at January 21, 2024

    From the following @excellproj 's comment,

    January 2024 curl -L "https://drive.usercontent.google.com/download?id=${fileId}&export=download&confirm=t" -o "file.zip" working great for me. Small and large files

    I checked this endpoint. By this, the following result is obtained. In the current stage, the following endpoint can be used.

    https://drive.usercontent.google.com/download?id={fileId}&confirm=xxx
    

    It seems that in the current stage, various values can be used to xxx of confirm=xxx. So, even confirm=xxx and confirm=yy and confirm=z can be used.

    The sample curl command is as follows.

    curl "https://drive.usercontent.google.com/download?id={fileId}&confirm=xxx" -o filename
    

    And/or,

    curl -L "https://drive.usercontent.google.com/download?id={fileId}&confirm=xxx" -o filename