2
votes

Using the list endpoint of the Google Drive api, I can retrieve the file ID and the mimetype of a Google Site file.

I tried to download the file and it fails. The response I get is "Only files with binary content can be downloaded. Use Export with Google Docs files." Makes sense.

Following the documentation here, I tired to export the file and it also fails. The response I get is:

com.google.api.client.http.HttpResponseException: 400 Bad Request
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "badRequest",
    "message": "The requested conversion is not supported.",
    "locationType": "parameter",
    "location": "convertTo"
   }
  ],
  "code": 400,
  "message": "The requested conversion is not supported."
 }
}

I tried it with the mimetype "application/pdf" and "application/zip" and "text/html" and still got the same response. What are the my export options here? I cannot find any documentation on this.

I'm having a similar problem with Google Form and Google Script files as well.

My Java code example:

Export export = driveService.files().export(fileId, "application/pdf");
export.executeMediaAndDownloadTo(outputStream);
2
For example, how about directly downloading from the URL of "application/pdf" of "exportLinks" retrieved by the method of Files: get in Drive API? developers.google.com/drive/api/v3/reference/files/get If this was not the direction you want, I apologize.Tanaike
Using the 'Try this API' feature at developers.google.com/drive/api/v3/reference/files/export, I was able to export a Google Docs file using this method to application/pdf, application/zip, text/html and application/vnd.openxmlformats-officedocument.wordprocessingml.document. Could you please provide more of your Java code so I can have a deeper look at your request?Rafa Guillermo
@RafaGuillermo great suggestion for the 'Try this API' feature, it captures my problem perfectly without using any Java code. I confirm that this endpoint works well for Google Docs, Slides and Spreadsheets. Now, I created a new Google Form in my Drive, and grabbed its file_id, and what should I put for the mimeType parameter to be able to export the Form?Alistair Jones
Actually, @Tanaike, you got me 95% of the way there! The "exportLinks" field of the files/get endpoint lists the mimetypes that are available for export, and I can use those mimetypes directly with the files/export endpoint. You can answer my question and I will accept it.Alistair Jones
You can use the application/zip MIME Type to export a Google Form this way. Also where are you finding the list of mimetypes available for export? The supported MIME Types listed here developers.google.com/drive/api/v3/mime-types are not supported for export.Rafa Guillermo

2 Answers

2
votes

By the recent Google's update, the property of exportLinks was also added to Drive API v3. This property had been able to be used at only Drive API v2 before. By this update, the URLs for exporting the converted files of Google Docs can be retrieved using the files.get method of Drive API v3. The endpoint for this method is as follows.

Endpoint:

GET https://www.googleapis.com/drive/v3/files/{fileId}?fields=exportLinks
  • When it requests to this URL, the blob can be downloaded.
  • For example, the URL for exporting Spreadsheet as a PDF file is as follows.

    - https://docs.google.com/spreadsheets/export?id={fileId}&exportFormat=pdf
    
  • You can also directly create the exporting URL like above.

    • When you use this, if the Google Docs file is not publicly shared, please use the access token retrieved from OAuth2 and/or Service Account. If the file is publicly shared, you can download the file using API key.

Reference:

1
votes

The Files: export method exports a Google Doc to a MIME Type specified in the request. This can be done with GET https://www.googleapis.com/drive/v3/files/<fileId>/export where fileId is the ID of the Document you wish to export.

The supported MIME Types of Google Drive files are listed here. When trying to export however, these types are not supported. If you want to get files of these MIME Types, then a simple GET request to the file’s resource URL can be used as in GET https://www.googleapis.com/drive/v3/files/<fieldId>?alt=media. The Files: export method will return

{
 "error": {
  "errors": [

    "domain": "global",
    "reason": "badRequest",
    "message": "The requested conversion is not supported.",
    "locationType": "parameter",
    "location": "convertTo"

  ],
  "code": 400,
  "message": "The requested conversion is not supported."

}

If you try to export to a Google file MIME Type.

You can however use the MIME Types specified here in order to export a Google doc. The MIME Types application/pdf, application/zip, text/html and application/vnd.openxmlformats-officedocument.wordprocessingml.document are all supported types, and can be tested using the ‘Try this API’ feature for Files: export.