【问题标题】:google drive api export谷歌驱动API导出
【发布时间】:2016-07-15 16:45:47
【问题描述】:

我是谷歌 API 的新手。我的目标是将文档(doc,docx)从服务器上传到谷歌驱动器,而无需任何客户端批准过程。上传后,我想将其导出为 pdf 文件。

我创建了一个服务帐户。上传本身工作正常。可以下载上传的文件。当我尝试从驱动器中导出文档时,出现以下错误。

{
  "error": {
    "errors": [
      {
        "domain": "global",
        "reason": "badRequest",
        "message": "ExportonlysupportsGoogleDocs."
      }
    ],
    "code": 400,
    "message": "ExportonlysupportsGoogleDocs."
  }
}

我使用 google drive api v2 php 库。这是我用于导出的代码。

    $tempUrl = "https://www.googleapis.com/drive/v2/files/xxxx/export?mimeType=application%2Fpdf";
    $request = new Google_Http_Request($tempUrl, 'GET', null, null);
    $httpRequest = $service->getClient()->getAuth()->authenticatedRequest($request);

    if ($httpRequest->getResponseHttpCode() == 200) {
      $newFile = 'downLoaded.pdf';
      file_put_contents($newFile, $httpRequest->getResponseBody());
    } else {
      var_dump($httpRequest->getResponseBody());
    }

不能从谷歌驱动器导出吗? 'ExportonlysupportsGoogleDocs.' 是什么意思?方法?我错过了什么?

感谢您的关注,欢迎提出任何意见。

【问题讨论】:

    标签: php google-drive-api export pdf-generation


    【解决方案1】:

    由于错误消息表明只能导出“Google Docs”文档。上传的文档应具有 mimeTypes given here 之一。

    对于使用 mimeType“application/vnd.google-apps.document”的 (Doc,Docx) 上传并尝试导出为 pdf。

    【讨论】:

      【解决方案2】:

      您只需将 Google 文档 mimeType 指定为云端硬盘文件。

      File fileMetadata = new File();
      fileMetadata.setName("My Report");
      fileMetadata.setMimeType("application/vnd.google-apps.spreadsheet");
      
      java.io.File filePath = new java.io.File("files/report.csv");
      FileContent mediaContent = new FileContent("text/csv", filePath);
      File file = driveService.files().create(fileMetadata, mediaContent)
      .setFields("id")
      .execute();
      System.out.println("File ID: " + file.getId());
      

      而且,400: Bad Request 表示它是User error。这可能意味着未提供必填字段或参数、提供的值无效或提供的字段组合无效。

      尝试将重复的父项添加到云端硬盘项目时可能会引发此错误。尝试添加会在目录图中创建循环的父级时也可能会抛出它。

      使用exponential backoff 的官方文档建议,指数退避是网络应用程序的标准错误处理策略,其中客户端在越来越长的时间内定期重试失败的请求。如果大量请求或繁重的网络流量导致服务器返回错误,指数退避可能是处理这些错误的好策略。相反,它不是处理与速率限制、网络容量或响应时间无关的错误的相关策略,例如无效的授权凭据或找不到文件错误。

      【讨论】:

      • 感谢您的回复。我确实指定了 mimeType。经过几个小时的工作,我发现我在上传时错过了“convert”=>“true”。这可以解决问题。没有这个声明,谷歌不允许我导出到其他格式。
      猜你喜欢
      • 2018-09-01
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多