【问题标题】:Google Drive download files with export apiGoogle Drive 使用导出 api 下载文件
【发布时间】:2021-12-20 16:39:41
【问题描述】:

上下文

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

然后我联系了 API-Explore,但我也无法下载任何内容。 有什么想法/提示吗?

注意我的...

  • SCOPES: 'https://www.googleapis.com/auth/drive'
  • 我在Credentials Page获得了一个开发者API Key
  • 我获得了OAuth 2.0 Client IDs

为你重现

公共你好世界文档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-api google-drive-api google-apis-explorer


    【解决方案1】:

    当我看到您的问题时,我注意到您要导出的文件是 Google Document,而您要导出的 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 Document 可以导出的 mimeTypes 时,可以使用 Drive API 中的“About:get”方法来实现。 Ref检索到Google Document的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"
        ]
      

    参考:

    【讨论】:

    • 感谢您重现并解决我的错误。现在可以了!
    • @ru4ert 感谢您的回复。我很高兴你的问题得到了解决。也谢谢你。
    猜你喜欢
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多