Google 通过导出驱动下载文件 api

Google Drive download files with export api

上下文

我想通过浏览器从 Google Drive 下载文件。 我可以通过 drive.get 访问文件的数据和详细信息,但无法导出文件。我没有在响应中看到 binaryString。

然后我联系了 API-Explore,但我也无法下载任何东西。 任何 ideas/hints?

注意我的...

为您转载

Public 你好世界文档 helloWorld

详情

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

id:'1O2ORp9te3FzqTFZkPLGNGvAnzcKBhdGZ-lu8TgABPrY'

API 资源管理器字符串

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

回应

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

本地代码

<!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>

参考和有用的链接

Drive Quickstart

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

当我看到你的问题时,我注意到你要导出的文件是 Google 文档,你要导出的 mimeType 是 application/vnd.google-apps.document。在Google Drive,不幸的是,在当前阶段,Google Docs 文件不能直接导出为Google Docs 的原始mimeType。例如,Google 文档无法导出为 Google 文档。这样就返回了The requested conversion is not supported.的错误。在这种情况下,需要将其转换为其他类型。比如是DOCX文件,PDF等等。这似乎是 Google 端的当前规范。

从上面的情况来看,为了去除你现在的问题,下面的修改怎么样?

发件人:

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

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

收件人:

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

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

mimeType:'application/pdf'

const mimeType = 'application/pdf'

注:

  • 如果想知道Google文档可以导出的mimeTypes,可以使用DriveAPI中的“About:get”方法实现。 Ref Google 文档的 exportFormats 的值被检索时,如下所示。

      [
        "application/rtf",
        "application/vnd.oasis.opendocument.text",
        "text/html",
        "application/pdf",
        "application/epub+zip",
        "application/zip",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "text/plain"
      ]
    

参考: