Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active November 13, 2024 14:30
Show Gist options
  • Save tanaikech/f0f2d122e05bf5f971611258c22c110f to your computer and use it in GitHub Desktop.
Save tanaikech/f0f2d122e05bf5f971611258c22c110f to your computer and use it in GitHub Desktop.
Downloading Shared Files on Google Drive Using Curl

Downloading Shared Files on Google Drive Using Curl

When the shared files on Google Drive is downloaded, it is necessary to change the download method by the file size. The boundary of file size when the method is changed is about 40MB.

File size < 40MB

CURL

filename="### filename ###"
fileid="### file ID ###"
curl -L -o ${filename} "https://drive.google.com/uc?export=download&id=${fileid}"

File size > 40MB

When it tries to download the file with more than 40MB, Google says to download from following URL.

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

Query included confirm=#### is important for downloading the files with large size. In order to retrieve the query from the HTML, it uses pup.

pup 'a#uc-download-link attr{href}'

And sed is used to remove amp;.

sed -e 's/amp;//g'`

So curl command is as follows.

CURL

filename="### filename ###"
fileid="### file ID ###"
query=`curl -c ./cookie.txt -s -L "https://drive.google.com/uc?export=download&id=${fileid}" | pup 'a#uc-download-link attr{href}' | sed -e 's/amp;//g'`
curl -b ./cookie.txt -L -o ${filename} "https://drive.google.com${query}"

The value of confirm is changed every time. So when the value of confirm is retrieved, the cookie is saved using -c ./cookie.txt, and when the file is downloaded, the cookie has to be read using -b ./cookie.txt.

By using this method, you can download files with the size of over 1 GB.

Reference: http://qiita.com/netwing/items/f2a595389f39206235e8

Update at March 21, 2019

sample script

This is a simple bash 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}

Reference: https://stackoverflow.com/questions/48133080/how-to-download-a-google-drive-url-via-curl-or-wget/48133859#48133859

CLI tool

If you will use a CLI tool, please use this. This is a CLI tool to download shared files and folders from Google Drive. For large file, the resumable download can be also run.

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}
  • In this case, the ID for downloading is retrieved from the HTML data as follows.

      <form id="downloadForm" action="https://drive.google.com/uc?export=download&amp;id={fileId}&amp;confirm={value for downloading}" method="post">
    
  • When you want to download a publicly shared file of small size from Google Drive, you can use the following command.

      curl -L "https://drive.google.com/uc?export=download&id=### fileId ###" -o sampleoutput.csv
    
  • Also, goodls was updated to v1.2.8.

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

Testing

  • August 3, 2022: I could confirm that the method on February 17, 2022, can be still available.
  • March 4, 2024: I could confirm that the method on January 21, 2024, can be still available.
@dckc
Copy link

dckc commented Aug 27, 2021

yes; thanks!

@tathastu871
Copy link

doesnt work anymore.

simple wget is enough

@ascorbic-acid
Copy link

working great as of 2022/2/25, thanks

@Green-cattow
Copy link

For MacOS use
curl -Lb ./cookie "https://drive.google.com/uc?export=download&`echo ${html} | grep -Eio '(confirm=[a-zA-Z0-9-_]+)'`&id=${fileid}" -o ${filename}

@tathastu871
Copy link

tathastu871 commented Feb 28, 2022

grep invalid range
chang to
confirm=[-a-zA-Z0-9_]

furthur it retrieves html not file

@tanaikech
Copy link
Author

@Green-cattow
Thank you for your comment. At windows, it seems that both -Po and -Eio can be used.

@tathastu871

grep invalid range
chang to
confirm=[-a-zA-Z0-9_]

furthur it retrieves html not file

Unfortunately, I cannote replicate your grep invalid range. When I tested my proposed script again, I confirmed that no error occurs. I deeply apologize for this.

For example, curl -s -L 'https://drive.google.com/uc?export=download&id={fileId}' | grep -Po '(confirm=[a-zA-Z0-9\-_]+)' returns confirm=###.

@tathastu871
Copy link

regex for character class says - must be added to last or first if itwas litereal for matching because in middle it acts as a range

@tanaikech
Copy link
Author

tanaikech commented Mar 4, 2022

@tathastu871 Thank you for the information. Can you provide the document site about your replying? I would like to study it.

@Apprisco
Copy link

Apprisco commented Aug 2, 2022

Hello, I'm running into a weird issue where it doesn't actually download the file: seems like quota issues or something.

@Apprisco
Copy link

Apprisco commented Aug 2, 2022

<title>Google Drive - Quota exceeded</title><style nonce="d11TWo0o1N-HHjsQ0tRdCQ">/* Copyright 2022 Google Inc

@heathen-pwn
Copy link

<title>Google Drive - Quota exceeded</title><style nonce="d11TWo0o1N-HHjsQ0tRdCQ">/* Copyright 2022 Google Inc

I confirm

<!DOCTYPE html><html><head><title>Google Drive - Quota exceeded</title><meta http-equiv="content-type" content="text/html; charset=utf-8"/><style nonce="16NTEEdYEYvBDTlTqenotA">/* Copyright 2022 Google Inc. All Rights Reserved. */ .goog-inline-block{position:relative;display:-moz-inline-box;display:inline-block}* html .goog-inline-block{display:inline}*:first-child+html .goog-inline-block{display:inline}.goog-link-button{position:relative;color:#15c;text-decoration:underline;cursor:pointer}.goog-link-button-disabled{color:#ccc;text-decoration:none;cursor:default}body{color:#222;font:normal 13px/1.4 arial,sans-serif;margin:0}.grecaptcha-badge{visibility:hidden}.uc-main{padding-top:50px;text-align:center}#uc-dl-icon{display:inline-block;margin-top:16px;padding-right:1em;vertical-align:top}#uc-text{display:inline-block;max-width:68ex;text-align:left}.uc-error-caption,.uc-warning-caption{color:#222;font-size:16px}#uc-download-link{text-decoration:none}.uc-name-size a{color:#15c;text-decoration:none}.uc-name-size a:visited{color:#61c;text-decoration:none}.uc-name-size a:active{color:#d14836;text-decoration:none}.uc-footer{color:#777;font-size:11px;padding-bottom:5ex;padding-top:5ex;text-align:center}.uc-footer a{color:#15c}.uc-footer a:visited{color:#61c}.uc-footer a:active{color:#d14836}.uc-footer-divider{color:#ccc;width:100%}</style><link rel="icon" href="null"/></head><body><div class="uc-main"><div id="uc-text"><p class="uc-error-caption">Sorry, you can&#39;t view or download this file at this time.</p><p class="uc-error-subcaption">Too many users have viewed or downloaded this file recently. Please try accessing the file again later. If the file you are trying to access is particularly large or is shared with many people, it may take up to 24 hours to be able to view or download the file. If you still can't access a file after 24 hours, contact your domain administrator.</p></div></div><div class="uc-footer"><hr class="uc-footer-divider"></div></body></html>

@horasjey
Copy link

how to get url direct download sir...?

@excellproj
Copy link

excellproj commented Jan 20, 2024

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

@tanaikech
Copy link
Author

@excellproj Thank you for your comment. From your comment, I could update it. Please confirm it.

@excellproj
Copy link

@tanaikech In my opinion, the confirm parameter can be kept in a simplified form for convenience: confirm=y

@tanaikech
Copy link
Author

@excellproj Thank you for replying.

@Ajinkgupta
Copy link

thank you @excellproj

@budimulia
Copy link

thanks is good!

@TROguz
Copy link

TROguz commented Mar 4, 2024

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 can't download. Would you share some example "original download" and curl download links?

@excellproj
Copy link

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 can't download. Would you share some example "original download" and curl download links?

Тhere's no difference between the original links and the curl link. You just need to replace ${fileId} with the file ID, which you can obtain from the file parameters on Google Drive. It might look like this: 17XznUk5h3eFuZZFvIYcFQzSIAvcnjmEo.
Example:
https://drive.usercontent.google.com/download?id=17XznUk5h3eFuZZFvIYcFQzSIAvcnjmEo&export=download&confirm=y

@j77h
Copy link

j77h commented Aug 8, 2024

August 2024:

I found that '&export' and '&confirm' are not required.

These 2 types of file behave differently:

  • Drive files (that were simply uploaded) use the URL form (as above) :
    https://drive.usercontent.google.com/download?id=${fileId}
    You cannot edit these; if you try, a new file is created.

  • Docs files (that appear in Drive after creating in Docs) :
    You can edit these, and the fileId remains the same after editing.
    When you get the link it ends with '/edit?usp=drive_link'
    and you change that to just '/export' in the curl URL:
    https://docs.google.com/document/d/${fileId}/export.
    It needs 'docs.google.com'; 'drive.usercontent.google.com' got an empty file.
    A plain text file was downloaded as html;
    to download as plain text, I added '?exportFormat=txt' :
    https://docs.google.com/document/d/${fileId}/export?exportFormat=txt
    'document' can be different, e.g. 'sheets'; use whatever is in the URL you get.

Complete command examples (bash) :

fileId='the id of a drive file' 
curl -L "https://drive.usercontent.google.com/download?id=${fileId}" -o 'test_output_1.txt'

fileId='the id of a doc' 
curl -L "https://docs.google.com/document/d/${fileId}/export?exportFormat=txt" -o 'test_output_2.txt'

@horasjey
Copy link

horasjey commented Aug 8, 2024

image
how to handle it..?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment