Joey Perroni
Joey Perroni

Reputation: 47

Is there a way to download shared files in Google Drive using their APIs?

I have files that have been shared with a Google Drive account that I can then access and download via the Google Drive APIs and display to a user in a browser. This has been working happily for the last few years.

Since the new shortcuts were introduced, the call I make to the API is no longer working for shared docs.

If I use the files->export call I get a 400 error "The requested conversion is not supported".

If I try the files->get call I get a 403 error "Only files with binary content can be downloaded. Use Export with Docs Editors files.".

The ability to download the content of a shared file was working up until the changes were made to introduce shortcuts.

Does anyone know of a method to use Google APIs to download contents of shared/shortcut files that are shared with my account?

Upvotes: 1

Views: 2241

Answers (1)

Tanaike
Tanaike

Reputation: 201768

I believe your goal is as follows.

  • The target file of the shortcut is Google Document.
  • You want to export a Google Document from a shortcut file.

Issue and workaround:

In the current stage, unfortunately, it seems that the Google Document of the target of the shortcut cannot be directly exported from the shortcut file. By this, when the shortcut file is exported with https://www.googleapis.com/drive/v3/files/{fileId of your shortcut file}/export?mimeType=application%2Fpdf, an error like The requested conversion is not supported. occurs. And, when the shortcut file is downloaded with https://www.googleapis.com/drive/v3/files/{fileId of your shortcut file}?alt=media, an error like Only files with binary content can be downloaded. Use Export with Docs Editors files. occurs.

In this case, in order to export the Document from the shortcut file, I thought that it is required to run the following process.

  1. Retrieve the target file ID from the shortcut file.
  2. Export the retrieved target file ID.

When this flow is reflected in a curl command, it becomes as follows.

1. Retrieve the target file ID from the shortcut file.

In this case, "Files: get" is used. As a sample fields, id,mimeType,name,shortcutDetails is used. shortcutDetails returns the information of the target file.

$ curl \
  -H 'Authorization: Bearer {your access token}' \
  'https://www.googleapis.com/drive/v3/files/{fileId of your shortcut file}?fields=id%2CmimeType%2Cname%2CshortcutDetails'

When this command is run, the following valur is returned.

{
  "id": "{fileId of your shortcut file}",
  "name": "filename",
  "mimeType": "application/vnd.google-apps.shortcut",
  "shortcutDetails": {
    "targetId": "{fileId of your Google Document file}",
    "targetMimeType": "application/vnd.google-apps.document"
  }
}

By this command, you can retrieve "targetId": "{fileId of your Google Document file}".

2. Export the retrieved target file ID.

In this sample, the Document is exported as PDF data. Here, please use the target ID. This ID is the file ID of Document.

$ curl \
  -H 'Authorization: Bearer {your access token}' \
   -o sample.pdf \
  'https://www.googleapis.com/drive/v3/files/{fileId of your Google Document file}/export?mimeType=application%2Fpdf'

By this command, the exported Document is saved as a PDF file.

Reference:

Upvotes: 2

Related Questions