3

Summary
Trying to download a JPEG file using files.export, but get a Error 403 message: "Export only supports Docs Editors files."

Description
I am trying to make use of Google Drive API for a project of mine, which is really all about collecting some pictures, storing them on the Drive, and later loading them back for some processing. Pretty straighforward.
To that extent, I am simply following the API Documentation and the example code snippets provided therein. Now, I was able to upload a picture to the storage. Trying to download it, I ran into this error message:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/1D1XpQwrCvOSEy_vlaRQMEGLQJK-AeeZ7/export?mimeType=image%2Fjpeg&alt=media returned "Export only supports Docs Editors files.". Details: "Export only supports Docs Editors files.">

The code use to upload the picture looks something like that:

 import io
 from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload

# GoogleDrive is a service created with googleapiclient.discovery.build

# upload a picture
file_metadata = {'name': 'example_on_drive.jpg'}
media = MediaFileUpload('example_upload.jpg', mimetype='image/jpeg')
file = GoogleDrive.files().create(
    body=file_metadata,
    media_body=media,
    fields='id').execute()

The upload is successful, I get back the file's ID:

print(file) 
{'id': '1aBoMvaHauCRyZOerNFfwM8yQ78RkJkDQ'}

and I can see it on my Google Drive.
Then I try to access that very same file:

request = GoogleDrive.files().export_media(fileId=file.get('id'), mimeType="image/jpeg")
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print("Download %d%%" % int(status.progress() * 100))

At which point the error occurs. I see the API Explorer on the website return the same error, which is why I am tempted to conclude that my code is not to blame. This file.export only has two inputs and I don't see how exactly I am failing to use it correctly. I looked through similar questions, but most of them deal with downloading text documents and my error message tells me it's got something to do with the doctype. Am I using a wrong mimeType?

Error handling suggestions on the official page do not feature this particular error message.

Have you got any suggestions?

dyma
  • 33
  • 1
  • 6

1 Answers1

12

From the error message of Export only supports Docs Editors files., in order to download the files except for Google Workspace documents (Document, Spreadsheet, Slides and so on), unfortunately, the method of "Files: export" cannot be used. In your case, when you want to download the image file, please use the method of "Files: get". When your script is modified, it becomes as follows.

From:

request = GoogleDrive.files().export_media(fileId=file.get('id'), mimeType="image/jpeg")

To:

request = GoogleDrive.files().get_media(fileId=file.get('id'))

Reference:

  • Download files
    • About the download a file using "export" and "get", you can see it at above official document.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • This doesn't actually download anything for me...And I'm confused, how is this the opposite answer from: https://stackoverflow.com/questions/71476202/google-drive-authenticate-and-download-files-with-service-account? – Ryan Farber Sep 15 '22 at 21:17
  • @Ryan Farber About `This doesn't actually download anything for me...And I'm confused, how is this the opposite answer from: stackoverflow.com/questions/71476202/…?`, I would like to support you. But, I cannot understand your situation. So, can you post it as a new question by including more information for correctly replicating `This doesn't actually download anything for me`? By this, I would like to confirm it. When you can cooperate to resolve your issue, I'm glad. Can you cooperate to do it? – Tanaike Sep 15 '22 at 22:50
  • Sorry for the confusion (fresh eyes helped): I've realized it did download actually download the file to a variable in memory...and I've now realized I need to do a subsequent step to save that bytecode to a file on my filesystem. And the other answer is for handling Google Docs etc :) – Ryan Farber Sep 16 '22 at 15:40
  • @Ryan Farber Thank you for replying. I'm glad your issue was resolved. – Tanaike Sep 16 '22 at 23:20