Search code examples
pythongoogle-drive-apimime-typesimagedownload

Google Drive API v3 files.export method throws a 403 error: "Export only supports Docs Editors files."


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?


Solution

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