google-apigoogle-drive-apigoogle-apis-explorer

Google Drive download files with export api


Context

I want to download files via the browser from Google Drive. I am able to access the files' data and details, via drive.get but I am not able to export the files. I didn't see a binaryString in the response.

Then I got in the touch with the API-Explore, where I'm not able to download anything too. Any ideas/hints?

Notice my...

Reproduce for you

Public hello world doc helloWorld

Details

mimeType:'application/vnd.google-apps.document'

id:'1O2ORp9te3FzqTFZkPLGNGvAnzcKBhdGZ-lu8TgABPrY'

API Explorer String

https://developers.google.com/drive/api/v3/reference/files/export?hl=en&apix_params=%7B%22fileId%22%3A%221O2ORp9te3FzqTFZkPLGNGvAnzcKBhdGZ-lu8TgABPrY%22%2C%22mimeType%22%3A%22application%2Fvnd.google-apps.document%22%7D#try-it

Response

{
 "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."
 }
}

local code

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta charset="utf-8" />
  <script type="text/javascript">
    var developerKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    var clientId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    var appId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    var scope = 'https://www.googleapis.com/auth/drive.drive'
    const mimeType = 'application/vnd.google-apps.document'
    const fileID = '1O2ORp9te3FzqTFZkPLGNGvAnzcKBhdGZ-lu8TgABPrY'

    function load() {
      gapi.load('client:auth2', () => {
        console.log(gapi)
        gapi.client
          .init({
            clientId,
            scope,
            apiKey: developerKey,
            discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'],
          })
          .then(
            () => {
              gapi.auth2.getAuthInstance().signIn()
              gapi.client.load('drive', 'v3', () => {
                gapi.client.drive.files
                  .export({
                    fileID,
                    mimeType,
                  })
                  .then(res => console.log(res))
                  .catch(error => console.error(error))
              })
            },
            error => console.error(error)
          )
      })
    }
  </script>
</head>

<body>
  <div id="result"></div>
  <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=load"></script>
</body>

</html>

Ref and useful links

Drive Quickstart

google-api-javascript-client/docs/reference.md


Solution

  • When I saw your question, I noticed that the file you want to export is Google Document and the mimeType you want to export is application/vnd.google-apps.document. At Google Drive, unfortunately, in the current stage, Google Docs files cannot be directly exported as the original mimeType of Google Docs. For example, Google Document cannot be exported as Google Document. By this, an error of The requested conversion is not supported. is returned. In this case, it is required to convert it to other type. For example, it's DOCX file, PDF, and so on. It seems that this is the current specification at the Google side.

    From the above situation, in order to remove the current issue of your situation, how about the following modification?

    From:

    mimeType:'application/vnd.google-apps.document'
    
    const mimeType = 'application/vnd.google-apps.document'
    

    To:

    mimeType:'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    
    const mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    

    and

    mimeType:'application/pdf'
    
    const mimeType = 'application/pdf'
    

    Note:

    Reference: