omarryhan/aiogoogle

Cannot export file with mimetype PDF

montessoriforall opened this issue · 6 comments

Hello! Here is a simple GDoc. The following code should allow one to export said GDoc from GDrive:

file_id = "1Mu179wAYfOgRc4SEvO1KTOZt78a24USLGIj3FpbAwxQ"
drive_v3 = await aiogoogle.discover("drive", "v3")

# this works
mimetype = "text/html"
response = await aiogoogle.as_user(drive_v3.files.export(fileId=file_id, mimeType=mime_type))
# response is str of html file

# this fails
mimetype = "application/pdf"
response = await aiogoogle.as_user(drive_v3.files.export(fileId=file_id, mimeType=mime_type))
# raises error

However, the lower code -- attempting to export a GDoc as a PDF file -- raises an error: ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: application/pdf'

Here's the full error:

Traceback (most recent call last):
  File "reportcards/venv/lib/python3.8/site-packages/aiogoogle/sessions/aiohttp_session.py", line 60, in resolve_response
    json = await response.json()
  File "reportcards/venv/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 1103, in json
    raise ContentTypeError(
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: application/pdf', url=URL('https://www.googleapis.com/drive/v3/files/1h5nHoJDqaYQyJu6MU28H7PS3ZYJ9gZ4kufgum46KgwY/export?mimeType=application/pdf')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "Library/Application Support/JetBrains/PyCharmCE2021.3/plugins/evaluate-async-code/_pydevd_async_debug.py", line 116, in manage_run
    yield
  File "Library/Application Support/JetBrains/PyCharmCE2021.3/plugins/evaluate-async-code/_pydevd_async_debug.py", line 70, in run_until_complete
    return f.result()
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/asyncio/futures.py", line 178, in result
    raise self._exception
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/asyncio/tasks.py", line 282, in __step
    result = coro.throw(exc)
  File "reportcards/_aiogoogle.py", line 50, in aiogDocExport
    response = await aiogoogle.as_user(drive_v3.files.export(fileId=file_id, mimeType=mime_type))
  File "reportcards/venv/lib/python3.8/site-packages/aiogoogle/client.py", line 243, in as_user
    return await self.send(
  File "reportcards/venv/lib/python3.8/site-packages/aiogoogle/client.py", line 387, in send
    return await self.active_session.send(*args, **kwargs)
  File "reportcards/venv/lib/python3.8/site-packages/aiogoogle/sessions/aiohttp_session.py", line 172, in send
    results = await schedule_tasks()
  File "reportcards/venv/lib/python3.8/site-packages/aiogoogle/sessions/aiohttp_session.py", line 164, in schedule_tasks
    return await asyncio.gather(*tasks, return_exceptions=False)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/asyncio/tasks.py", line 349, in __wakeup
    future.result()
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/asyncio/tasks.py", line 280, in __step
    result = coro.send(None)
  File "reportcards/venv/lib/python3.8/site-packages/aiogoogle/sessions/aiohttp_session.py", line 150, in get_content
    response = await get_response(request)
  File "reportcards/venv/lib/python3.8/site-packages/aiogoogle/sessions/aiohttp_session.py", line 144, in get_response
    response = await resolve_response(request, response)
  File "reportcards/venv/lib/python3.8/site-packages/aiogoogle/sessions/aiohttp_session.py", line 63, in resolve_response
    data = await response.text()
  File "reportcards/venv/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 1085, in text
    return self._body.decode(  # type: ignore[no-any-return,union-attr]
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd3 in position 10: invalid continuation byte

Is there another way to export files from aiogoogle as PDF that I am unaware from?

Hey, yes, you need to specify the download_file argument, because without it, Aiogoogle assumes what you're fetching is JSON.

download_file="/home/foo/Downloads" will download the file in /home/foo/Downloads

Hey Omar:

Thank you so much! This perfectly solves my problem, though I will protest that the error message is a bit cryptic. Thanks for your excellent work on this package! :)

Thank you @montessoriforall! :)
Agreed, it's a bit confusing. Maybe we can make it clearer in the docs how to download/upload files?

We currently only have examples e.g.: https://aiogoogle.readthedocs.io/en/latest/#download-a-file-from-google-drive.

Documenting the file upload & download feature outside of the examples section might be the correct thing to do.

Hey @omarryhan, how are you?
Is it possible to get the file (in my case is an image) and keep it in memory as an array of bytes for example?

I just tried to do it but Im keeping short :(

        async with aiogoogle:
            drive_v3 = await aiogoogle.discover('drive', 'v3')

            res = await aiogoogle.as_service_account(
                drive_v3.files.get(fileId=file_id, download_file='/image.png', alt='media')
            )  

I tested the sync way and it worked perfectly, but I would like to do the same async.

Thanks :)

Hey @peyafaguerre

Have you tried using the pipe_to parameter?

You can find some usage examples here #98

Thanks @omarryhan!
It works perfectly!