wkentaro / gdown

Google Drive Public File Downloader when Curl/Wget Fails
MIT License
4.18k stars 348 forks source link

Permission denied ... Maybe you need to change permission over 'Anyone with the link'? #43

Open alexklwong opened 4 years ago

alexklwong commented 4 years ago

I want to download a 16GB zip file on Google Drive using:

gdown https://drive.google.com/uc?id=1InfIal4y7OBMGNUDeldEmDxtD0MrewY8

I have already set the permissions on the file to ``Anyone with the link''

But I get this error:

Permission denied: https://drive.google.com/uc?id=1InfIal4y7OBMGNUDeldEmDxtD0MrewY8 Maybe you need to change permission over 'Anyone with the link'?

MHDBST commented 2 years ago

In case you have a folder: gdown --folder --no-cookies --id XXX worked for me

YueSun0609 commented 2 years ago

Use gdrive can solve it.

JamesCallanan commented 2 years ago

I wanted to import a file from my google drive onto google colaboratory. The work around I used was to mount my google drive and move the file using the mv command.

Might be helpful for someone.

from google.colab import drive
drive.mount('/content/gdrive')
!cp '/content/gdrive/MyDrive/New Research/training.zip' 'training.zip'
INF800 commented 2 years ago

Answer is simple!!!!

These happen to files added as shortcuts.

Soultion:

Upload file in drive manually and try to access it's link

wkentaro commented 2 years ago

This probably fixes some of the problems after many accesses to a particular file. https://github.com/wkentaro/gdown/issues/146#issuecomment-1042382215

https://github.com/wkentaro/gdown/pull/147 will include it.

wkentaro commented 2 years ago

Gdown v4.3.0 is out! https://github.com/wkentaro/gdown/releases/tag/v4.3.0 Hope it fixes this issue. (Please request reopening if not)

FYI, you can upgrade gdown by:

pip install --upgrade --no-cache-dir gdown
ahmedshahriar commented 2 years ago

Gdown v4.3.0 is out! https://github.com/wkentaro/gdown/releases/tag/v4.3.0 Hope it fixes this issue. (Please request reopening if not)

FYI, you can upgrade gdown by:

pip install --upgrade --no-cache-dir gdown

it solved for me , Thanks

elibtronic commented 2 years ago

Gdown v4.3.0 is out! https://github.com/wkentaro/gdown/releases/tag/v4.3.0 Hope it fixes this issue. (Please request reopening if not)

FYI, you can upgrade gdown by:

pip install --upgrade --no-cache-dir gdown

Fixed for me as well. Thanks.

puyuanliu commented 2 years ago

Gdown v4.3.0 is out! https://github.com/wkentaro/gdown/releases/tag/v4.3.0 Hope it fixes this issue. (Please request reopening if not)

FYI, you can upgrade gdown by:

pip install --upgrade --no-cache-dir gdown

Thanks a lot! FIxed for me!

ibracorp-sycotix commented 2 years ago

Gdown v4.3.0 is out! https://github.com/wkentaro/gdown/releases/tag/v4.3.0 Hope it fixes this issue. (Please request reopening if not)

FYI, you can upgrade gdown by:

pip install --upgrade --no-cache-dir gdown

Thank you

sunotsue commented 2 years ago

Still doesn't work - I'm hit with the following error message:

Access denied with the following error:

Cannot retrieve the public link of the file. You may need to change
the permission to 'Anyone with the link', or have had many accesses. 

You may still be able to access the file from the browser:

fdila commented 2 years ago

Hi, I'm having the same problem with gdown==4.4.0, while gdown==4.3.0 works fine on the same files.

sunotsue commented 2 years ago

After trying multiple different hacks on stackoverflow and on this repo, I failed to get the job done with gdown and had to use a custom python function from a stackoverflow post, which worked like magic.

import requests
def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)    

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value

    return None

def save_response_content(response, destination):
    CHUNK_SIZE = 32768

    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

if __name__ == "__main__":
    file_id = 'TAKE ID FROM SHAREABLE LINK'
    destination = 'DESTINATION FILE ON YOUR DISK'
    download_file_from_google_drive(file_id, destination) 
redkur commented 2 years ago
Sorry...
Google
Sorry...

We're sorry...

... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now.

See Google Help for more information.

Google Home
JonathanLehner commented 2 years ago

not funny... doesn't work

mtkennerly commented 2 years ago

I've just started running into this, but it happens sporadically. For me, there's no difference between using 4.4.0 and 4.3.0, and there's no difference between using just the ID or https://drive.google.com/open?id= or https://drive.google.com/uc?id= or https://drive.google.com/u/0/uc?id=.

The affected file ID is 1_gBUGKhs_bponRcgpLlukggWPCR25blF (2.8 MB .tar.gz). I'm using gdown in a GitHub workflow, which had no issues as of 16 days ago, and then there weren't any workflow runs until 23 hours ago, which started failing. There are multiple jobs within the workflow, and sometimes gdown will work in a few jobs but not in others. The pass/fail number varies.

When I run it locally, it also works a few times and then starts failing for a bit. I haven't noticed a pattern about timing or consecutive downloads.

Edit: It also happens sporadically with curl -v -L -o ~/fossil.tgz https://drive.google.com/uc?id=1_gBUGKhs_bponRcgpLlukggWPCR25blF, so it's not specific to gdown. I wonder if anything changed on Google's side recently to make this more sensitive.

When it fails

Notable headers:

    "Access-Control-Allow-Methods": "GET,HEAD,OPTIONS",
    "Content-Length": "0",
    "Content-Type": "text/html; charset=UTF-8",

When it works

Notable headers:

    "Access-Control-Allow-Methods": "GET,OPTIONS",  
    "Content-Type": "application/x-gzip",   
    "Content-Disposition": "attachment;filename=\"fossil-linux-x64-2.13.tar.gz\";filename*=UTF-8''fossil-linux-x64-2.13.tar.gz",    
    "Content-Length": "2915857",    
kinoc commented 2 years ago

While trying to prefetch the file name and other metadata I ran into something similar. However, going to the eventual redirect URL seemed to fix it.

URL = "https://drive.google.com/uc?id="+id+"&authuser=0&export=download&confirm=t"

Adding the "&confirm=t" seemed to help and get straight to the info with Content-Disposition in the header

def fetch_file_metadata_from_google_drive(id):
    URL = "https://drive.google.com/uc?id="+id+"&authuser=0&export=download&confirm=t"
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"  # NOQA
    }
    verify=True
    url_origin = URL
    session = requests.Session()
    meta={}
    meta['id']=id
    meta['valid']=False
    meta['filename']=''
    meta['url']=URL

    response = session.get(URL,allow_redirects=True, headers=headers, stream = True,verify=verify)
    token = get_confirm_token(response)
    meta['token']=token
    for k in response.headers:
        meta[k] = str(response.headers[k])

    if ('Content-Disposition' in response.headers):
        # Get the filename from the response header 'Content-Disposition'
        match = re.search(r'filename="(?P<filename>.+)"', response.headers['Content-Disposition'])
        # Make sure we found the filename field inside Content-Disposition
        if match is None:
            print('\n\nERROR: Unable to retrieve `dest_path` from `file_id`, please set it manually.')
            return meta
        if platform == 'win32':
            # Make it Windows safe, stripping: \/<>:"|?*
            remove_characters = dict((ord(char), None) for char in '\\/<>:"|?*')
        else:
            # Make it macOS and linux safe, stripping: /
            remove_characters = dict((ord(char), None) for char in '/')

        dest_path = match['filename'].translate(remove_characters)
        meta['filename']=dest_path
        meta['valid']=True
        print(json.dumps(meta,indent=4))
    else:
        # Need to redirect with confirmation
        try:
            url = get_url_from_gdrive_confirmation(response.text)
        except RuntimeError as e:
            print("Access denied with the following error:")
            error = "\n".join(textwrap.wrap(str(e)))
            error = indent(error, "\t")
            print("\n", error, "\n", file=sys.stderr)
            print(
                "You may still be able to access the file from the browser:",
                file=sys.stderr,
            )
            print("\n\t", url_origin, "\n", file=sys.stderr)
            meta['browser_url']=url_origin 
            return meta
        print("redirect_url:",id,"-->",url)
        meta['redirect_url']=url        
    return meta
kinoc commented 2 years ago

While the last snippet worked better, I still experienced the permission denied error after a while, and suspect it may be due to some google side throttling / bot detection mechanism for bulk downloads. For singles or small batches it wouldn't trip but for a large number of individual files, it might trip. In any case, the random backoff code that use the above metadata fetch seems to be having much better luck for me. Of course it is much slower, but it makes steady progress and doesn't get blocked. I think the pause between downloads might have the biggest effect on the detection mechanism, if there is one. All just a wild guess that requires testing by the suitably motivated user base ...

url_set is a list of file id's

import random
import time 
...
for file_id in url_set:
    meta = fetch_file_metadata_from_google_drive(file_id)
    lowspeed = 300 * 1024 
    highspeed = 1024 * 1024 
    speed = int(random.uniform(lowspeed,highspeed))
    sleeptime = random.uniform(5.0,12.0)
    if (meta['valid'] and len(meta['filename'])>0):
        epath = dest_path + meta['filename']
        if (not os.path.exists(epath)):
            gdown.download(meta['url'],output=dest_path,quiet=False,speed = speed)
            sleeptime = random.uniform(8.0,32.0)
        else:
            print("ALREADY HAVE:", meta['filename'])
    else:
        print("INVALID:",file_id)
    print("Pausing for:",sleeptime)
    time.sleep(sleeptime)
    print("-----")
SalimF commented 2 years ago

When I add &confirm=t I got this error ,

Access denied with the following error: 
    Cannot retrieve the public link of the file. You may need to change
    the permission to 'Anyone with the link', or have had many accesses.  
You may still be able to access the file from the browser:

The file are acceptable via my browser, but I'm using VPS to download/convert files , I need to download files from command line .

janzd commented 2 years ago

I get the same errors. Either:

Cannot retrieve the public link of the file. You may need to change the permission to 'Anyone with the link', or have had many accesses.

or:

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.       

Btw it's not even a public dataset. It's my own dataset that I uploaded on google drive and nobody has ever downloaded it yet. Not even I. I got the error the first time I tried to download it. The files are accessible through browser, though.

Investigamer commented 2 years ago

So I develop an app that utilizes large Photoshop templates (about 1gb each) which I host on Google Drive, I tried going the official route of using Drive API and Google Auth but google does not seem to be interested in verifying my app so I turned to Gdown.

It seems however that my files are being rate limited, and past a certain point users of my app just can't download the templates, I'm getting the same errors outlined above. Is there currently no way around these daily access limitations?

duckduckgrayduck commented 1 year ago

Sadly I reached the same issue that @janzd and @mtkennerly are experiencing. It would sporadically work. I don't think it's specific to gdown either. I think it is Google rate limiting/bot detection. I experience it much more regularly with folder download.

Investigamer commented 1 year ago

Sadly I reached the same issue that @janzd and @mtkennerly are experiencing. It would sporadically work. I don't think it's specific to gdown either. I think it is Google rate limiting/bot detection. I experience it much more regularly with folder download.

In my app I ended up having gdrive download as the first attempt, and if the download fails I reroute to an Amazon S3 bucket download for a file of the same name. I know its not ideal for some projects but it allows gdrive to offset much of the storage costs with amazon s3 as a fallback at a small cost (in my case 14c per month storing about 20GB).

zhanghm1995 commented 1 year ago

I also encountered this problem almost all the time when I want to down big files in Google Drive. And the solutions mentioned above do not work for me as well. Finally, I successfully tried using OAuth 2.0 to generate the tokens to download a big file in Google Drive.

The details can be referred to from this link. So I create a download.sh script, and add the following commands:

ACCESS_TOKEN="Your_Access_Token"

curl -H "Authorization: Bearer ${ACCESS_TOKEN}" https://www.googleapis.com/drive/v3/files/{FILE_ID}?alt=media -o data.tar.gz

Please replace the ACCESS_TOKEN and FILE_ID with yours.

And then, execute the command bash download.sh in a terminal, and everything goes well : )

yashsandansing commented 1 year ago

Maybe the developers at gdown should take a look at this issue that is persistent from 2 years and not rely on people making workarounds!!!

TomoshibiAkira commented 1 year ago

Maybe the developers at gdown should take a look at this issue that is persistent from 2 years and not rely on people making workarounds!!!

its literally not gdown's fault. that being said, maybe adding an option to use the oauth 2.0 token that user provides could potentially solve the problem.

JustJerem commented 1 year ago

I tried the solution provided by Mehdi and it's working !

!gdown "YOUR_ID_HERE&confirm=t"

wkentaro commented 1 year ago

Maybe the developers at gdown should take a look at this issue that is persistent from 2 years and not rely on people making workarounds!!!

its literally not gdown's fault. that being said, maybe adding an option to use the oauth 2.0 token that user provides could potentially solve the problem.

Thanks for the suggestion. I didn't think of that, but that could be a nice solution to this issue.

batara666 commented 1 year ago

pip3 install --upgrade gdown solve my issue

XLechter commented 1 year ago

I tried the solution provided by Mehdi and it's working !

!gdown "YOUR_ID_HERE&confirm=t"

This also works for me. A very simple way.

batbilguun44 commented 1 year ago

!gdown "YOUR_ID_HERE&confirm=t"

Worked like a charm. thank you.

emwaza commented 1 year ago

pip3 install --upgrade gdown solve my issue

This worked like a charm. thank you so much

thecodemango commented 1 year ago

pip3 install --upgrade gdown solve my issue

This worked for me as well. I just put !pip3 install --upgrade gdownat the beginning of the code (I'm using Google Colab)

AlexGrunt commented 1 year ago

pip3 install --upgrade gdown solve my issue

Worked for me as well, thank you

marat1804 commented 1 year ago

pip3 install --upgrade gdown solve my issue

Thx. Solved my problem :D

Investigamer commented 1 year ago

So everyone is saying the error is solved but, am I correct in assuming that the issue can still happen if Google rate limits your files? I have about 17gb of files my app tries to download using gDown and the more people use my app the less they are able to download these files, and I assume its due to google rate limiting the files themselves. There's no way around that is there?

0-hero commented 1 year ago

pip3 install --upgrade gdown solve my issue

Fixed the issue

acoiman commented 1 year ago

I've added a new feature to keep cookies #51. I hope this fixes this issue.

You can try pre-released version of this.

pip install -U --no-cache-dir gdown --pre

by default, cookies is enabled and saved into ~/.cache/gdown/cookies.json. You can disable it by gdown --no-cookies XXX

It works

luchaoqi commented 1 year ago

So everyone is saying the error is solved but, am I correct in assuming that the issue can still happen if Google rate limits your files? I have about 17gb of files my app tries to download using gDown and the more people use my app the less they are able to download these files, and I assume its due to google rate limiting the files themselves. There's no way around that is there?

I believe this is correct. At this moment, I tried all the answers in this thread but unfortunately, none of them worked for me. Seems google is limiting the download somehow. File link fyi: https://drive.google.com/file/d/1eDjh-_bxKKnEuz5h-HXS7EDJn59clx6V/view

AnirudhJayant06 commented 1 year ago

In google colab try this, it solved my issue

!pip install --upgrade --no-cache-dir gdown

Xinyu-Li-123 commented 1 year ago

In google colab try this, it solved my issue

!pip install --upgrade --no-cache-dir gdown

Work like a charm 👍

MJAHMADEE commented 1 year ago

I want to download a 16GB zip file on Google Drive using:

gdown https://drive.google.com/uc?id=1InfIal4y7OBMGNUDeldEmDxtD0MrewY8

I have already set the permissions on the file to ``Anyone with the link''

But I get this error:

Permission denied: https://drive.google.com/uc?id=1InfIal4y7OBMGNUDeldEmDxtD0MrewY8 Maybe you need to change permission over 'Anyone with the link'?

Try this: !pip install --upgrade --no-cache-dir gdown

ShadeRedeye commented 1 year ago

I want to download a 16GB zip file on Google Drive using: gdown https://drive.google.com/uc?id=1InfIal4y7OBMGNUDeldEmDxtD0MrewY8 I have already set the permissions on the file to ``Anyone with the link'' But I get this error: Permission denied: https://drive.google.com/uc?id=1InfIal4y7OBMGNUDeldEmDxtD0MrewY8 Maybe you need to change permission over 'Anyone with the link'?

Try this: !pip install --upgrade --no-cache-dir gdown

looks like these method didn't working now

Investigamer commented 1 year ago

looks like these method didn't working now

It works for some causes, the problem is if your file is being accessed a certain number of times a day there's no way to fix the issue other than using Oauth, Google Drive rate limits file downloads without authentication.

matheusschreiber commented 1 year ago

I want to download a 16GB zip file on Google Drive using: gdown https://drive.google.com/uc?id=1InfIal4y7OBMGNUDeldEmDxtD0MrewY8 I have already set the permissions on the file to ``Anyone with the link'' But I get this error: Permission denied: https://drive.google.com/uc?id=1InfIal4y7OBMGNUDeldEmDxtD0MrewY8 Maybe you need to change permission over 'Anyone with the link'?

Try this: !pip install --upgrade --no-cache-dir gdown

This did the trick for me. But it was a private document, not many access...

wayne931121 commented 1 year ago

Google Drive Anonymous downloads have a daily limit, if exceed, it will say:

Sorry, you can't view or download this file at this time.

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.

Maybe 4.8GB per file (per day), I write this script to test, when downloaded very big file, it only download 4.8GB. Script: https://github.com/wayne931121/download_googledrive_file

To solve this problem, you can provide cookie to log in, the same cookie in the same time can only visite once, or server will response 429 Too Many Requests (See Here: HTTP error 429 (Too Many Requests))

Way2 is to use other cloud drive to solve this problem.

Way3 is to split your large file to many small file to download them, this is the best way I suggest, I also write a script to do it(See Here: https://github.com/wkentaro/gdown/issues/26#issuecomment-1356670389)

Rakesh3929 commented 1 year ago

I am trying to download this folder - https://drive.google.com/drive/folders/1NVqQApjO-u881sMTSNubNyn-AogC1q04

But i am getting an error even after trying the solution provided here. I tried like this,

gdown "https://drive.google.com/drive/folders/1NVqQApjO-u881sMTSNubNyn-AogC1q04&confirm=t".

But this throws up the error - C:\Program Files\Python311\Lib\site-packages\gdown\parse_url.py:44: UserWarning: You specified a Google Drive link that is not the correct link to download a file. You might want to try --fuzzy option or the following url: https://drive.google.com/uc?id=None warnings.warn( Downloading... From: https://drive.google.com/drive/folders/1NVqQApjO-u881sMTSNubNyn-AogC1q04&confirm=t To: C:\1NVqQApjO-u881sMTSNubNyn-AogC1q04&confirm=t 1.69kB [00:00, ?B/s].

Can somebody help me?

amanikiruga commented 1 year ago

The solution that worked for me is just to make a copy of the file in Google Drive and use that instead. Remember to set permissions to "Anyone with the Link can view"

kwea123 commented 1 year ago

My solution is (same as above more or less)

  1. Click "Make A Copy" on google drive. It will create a copy of the file (either it's yours or other people's) to your drive.
  2. Set the permission to "Anyone with the link" as "Viewer".
  3. Now you can download as usual.

This works for single files or a folder. The problem is that the same error will occur if you are downloading a folder containing too many files. In that case, you can delete the folder and do the above process again, in this way, google will recognize them as different files so it doesn't err like "Too many users have viewed or downloaded".

The drawback of this is that it requires manual operation once the error occurs again.

IMO this is related to google drive not gdown so there is no point to do those pip install stuff

leo-frank commented 1 year ago

I try this method: instead of gdown <drive-id> type gdown "<drive-id>&confirm=t" as here: https://stackoverflow.com/questions/60739653/gdown-is-giving-permission-error-for-particular-file-although-it-is-opening-up-f#comment131605319_65347090.

It succeed!