繁体   English   中英

谷歌驱动器api导出

[英]google drive api export

我是google api的新手。 我的目标是在没有任何客户审批流程的情况下将文档(doc,docx)从服务器上传到Google驱动器。 上传后,我想将其导出为pdf文件。

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

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

我使用谷歌驱动器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'。 手段? 我错过了什么?

感谢您的关注和任何评论表示赞赏。

正如错误消息所示,只能导出“Google Docs”文档。 上传的文档应该有一个这里给出的mimeTypes。

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

您只需将Google文档mimeType指定为Drive文件即可。

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 这可能意味着未提供必填字段或参数,提供的值无效,或提供的字段组合无效。

尝试将重复的父项添加到Drive项时,可能会抛出此错误。 当尝试添加将在目录图中创建循环的父级时,也可以抛出它。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM