iterative / PyDrive2

Google Drive API Python wrapper library. Maintained fork of PyDrive.
https://docs.iterative.ai/PyDrive2
Other
581 stars 69 forks source link

How to download Sheets? "The requested conversion is not supported" error #149

Closed drscotthawley closed 2 years ago

drscotthawley commented 3 years ago

Hi folks. Thanks for your work on this. I'm trying to download file content using GetContent. The file I'm trying to get is a Google Sheets file from its sharing URL ("Anyone with the link can view"). But apparently pyDrive2 is trying to request it as a text file instead of just downloading it. And this generates an error about "only binary files can be downloaded." Uh....? I just want to download a file programmatically, without having to manually use the Export button. If you don't try to convert to text, it should work (e.g. I can do this via wget or curl no problem), but pydrive2 seems to be trying to get it to be converted to a text file?

The url in question is https://docs.google.com/spreadsheets/d/1bwgUL14KCHbBDZErUPScLNYAjb_elb_5pnzHxNyNxY0/edit?usp=sharing

From which I get the id = "1bwgUL14KCHbBDZErUPScLNYAjb_elb_5pnzHxNyNxY0".

Then I run

gd_file = drive.CreateFile({'id': id})
gd_file.GetContentFile('spreadsheet.xlsx')

and that's when the error occurs:

Traceback (most recent call last):
  File "/home/shawley/envs/fastai/lib/python3.9/site-packages/pydrive2/files.py", line 336, in GetContentFile
    download(fd, files.get_media(fileId=file_id))
  File "/home/shawley/envs/fastai/lib/python3.9/site-packages/pydrive2/files.py", line 326, in download
    status, done = downloader.next_chunk()
  File "/home/shawley/envs/fastai/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 131, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/shawley/envs/fastai/lib/python3.9/site-packages/googleapiclient/http.py", line 779, in next_chunk
    raise HttpError(resp, content, uri=self._uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v2/files/1bwgUL14KCHbBDZErUPScLNYAjb_elb_5pnzHxNyNxY0?alt=media returned "Only files with binary content can be downloaded. Use Export with Docs Editors files.". Details: "[{'domain': 'global', 'reason': 'fileNotDownloadable', 'message': 'Only files with binary content can be downloaded. Use Export with Docs Editors files.', 'locationType': 'parameter', 'location': 'alt'}]">

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/shawley/envs/fastai/lib/python3.9/site-packages/pydrive2/files.py", line 347, in GetContentFile
    download(
  File "/home/shawley/envs/fastai/lib/python3.9/site-packages/pydrive2/files.py", line 326, in download
    status, done = downloader.next_chunk()
  File "/home/shawley/envs/fastai/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 131, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/shawley/envs/fastai/lib/python3.9/site-packages/googleapiclient/http.py", line 779, in next_chunk
    raise HttpError(resp, content, uri=self._uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v2/files/1bwgUL14KCHbBDZErUPScLNYAjb_elb_5pnzHxNyNxY0/export?mimeType=text%2Fplain&alt=media returned "The requested conversion is not supported.". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'The requested conversion is not supported.', 'locationType': 'parameter', 'location': 'convertTo'}]">

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/shawley/Dropbox/Teaching/DL_AEI_3895/Fall21/autograding/./autograder.py", line 76, in <module>
    download_if_newer_gdrive(ss_url, 'responses.xlsx')
  File "/home/shawley/Dropbox/Teaching/DL_AEI_3895/Fall21/autograding/./autograder.py", line 69, in download_if_newer_gdrive
    gd_file.GetContentFile(dst_file)
  File "/home/shawley/envs/fastai/lib/python3.9/site-packages/pydrive2/auth.py", line 84, in _decorated
    return decoratee(self, *args, **kwargs)
  File "/home/shawley/envs/fastai/lib/python3.9/site-packages/pydrive2/files.py", line 352, in GetContentFile
    raise ApiRequestError(error)
pydrive2.files.ApiRequestError: <HttpError 400 when requesting https://www.googleapis.com/drive/v2/files/1bwgUL14KCHbBDZErUPScLNYAjb_elb_5pnzHxNyNxY0/export?mimeType=text%2Fplain&alt=media returned "The requested conversion is not supported.". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'The requested conversion is not supported.', 'locationType': 'parameter', 'location': 'convertTo'}]">

Is this an issue related to BOM? I didn't understand that part of the docs or the example. I might just use wget since that works fine, but it'd be nice to stay in pydrive2.

Thanks!

MateoWartelle commented 3 years ago

can you try specifying mimeType ?

from pydrive2.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

id = "1bwgUL14KCHbBDZErUPScLNYAjb_elb_5pnzHxNyNxY0"
gd_file = drive.CreateFile({'id': id})
gd_file.GetContentFile('spreadsheet.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
yeralexey commented 2 years ago

I am new at Github, so i am not getting correct way to start issue, if it seems that i face same trouble, discussed in closed issue, so i write here, correct me if i am wrong.

so this is code i use to collect tracebakcs, it gets "mimetype" from Listfile(), when i ise "for item in file_list" it is in item['mimeType'].

def getfile(fileid, filename, mimetype):
    try:
        file = go.CreateFile({'id': fileid})
        file.GetContentFile(f"{save_path}/{filename}", f"{mimetype}") #, chunksize=26214400
        print(f"ok {filename}, {mimetype}")
    except:
        print(f"no, {filename}, {mimetype}")
        with open("traceback.txt", "a", encoding='utf-8') as file:
            file.write(f"{filename}, {mimetype} _______________________________________________________________")
        with open("traceback.txt", "a", encoding='utf-8') as file:
            file.write(traceback.format_exc())

so i do send mimetype, the only differ i see is, that my filename does not have ".xlsx" at the end. Code works for all types of files except Google, spreadsheets, docs, maps... So here comes traceback, i removed the function name and etc, to make it more laconic:

readme, application/vnd.google-apps.document _______________________________________________________________Traceback (most recent call last):
googleapiclient.errors.HttpError: <HttpError 403 "Only files with binary content can be downloaded. Use Export with Docs Editors files.". Details: "[{'domain': 'global', 'reason': 'fileNotDownloadable', 'message': 'Only files with binary content can be downloaded. Use Export with Docs Editors files.', 'locationType': 'parameter', 'location': 'alt'}]">
googleapiclient.errors.HttpError: <HttpError 400 "The requested conversion is not supported.". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'The requested conversion is not supported.', 'locationType': 'parameter', 'location': 'convertTo'}]">
pydrive2.files.ApiRequestError: <HttpError 400 "The requested conversion is not supported.". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'The requested conversion is not supported.', 'locationType': 'parameter', 'location': 'convertTo'}]">

testspreadsheet, application/vnd.google-apps.spreadsheet _______________________________________________________________Traceback (most recent call last):
googleapiclient.errors.HttpError: <HttpError 403 "Only files with binary content can be downloaded. Use Export with Docs Editors files.". Details: "[{'domain': 'global', 'reason': 'fileNotDownloadable', 'message': 'Only files with binary content can be downloaded. Use Export with Docs Editors files.', 'locationType': 'parameter', 'location': 'alt'}]">
googleapiclient.errors.HttpError: <HttpError 400 "The requested conversion is not supported.". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'The requested conversion is not supported.', 'locationType': 'parameter', 'location': 'convertTo'}]">
pydrive2.files.ApiRequestError: <HttpError 400 "The requested conversion is not supported.". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'The requested conversion is not supported.', 'locationType': 'parameter', 'location': 'convertTo'}]">

Teriberka, application/vnd.google-apps.map _______________________________________________________________Traceback (most recent call last):
googleapiclient.errors.HttpError: <HttpError 403 "Only files with binary content can be downloaded. Use Export with Docs Editors files.". Details: "[{'domain': 'global', 'reason': 'fileNotDownloadable', 'message': 'Only files with binary content can be downloaded. Use Export with Docs Editors files.', 'locationType': 'parameter', 'location': 'alt'}]">
googleapiclient.errors.HttpError: <HttpError 400 "Export only supports Docs Editors files.". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'Export only supports Docs Editors files.'}]">
pydrive2.files.ApiRequestError: <HttpError 400 "Export only supports Docs Editors files.". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'Export only supports Docs Editors files.'}]">

so fist comes file name, next goes mimetype...

What am i doing wrong, please help...

@shcheklein @MateoWartelle @drscotthawley @tasdomas @rlamy