【问题标题】:Google DriveService API Upload with mimetype application/vnd.google-apps.document failing使用 mimetype application/vnd.google-apps.document 上传 Google DriveService API 失败
【发布时间】:2020-11-21 20:39:22
【问题描述】:

我正在尝试将文件上传到 Google 云端硬盘并通过 Google 文档共享/访问该文件。我已经复制了下面的代码 sn-p。我正在使用 Google Drive API V3。它失败并出现错误 GoogleApiException:

Google.Apis.Requests.RequestError 错误请求 [400]。

不确定请求中缺少什么。我尝试了几个 File 的附加参数,但它总是返回 Bad Request Error。如果有人知道解决方案,请告诉我。

string fileType = "application/vnd.google-apps.document";
var newFile = new File
      {
          Name = title,
          MimeType = fileType,
          Description= title,          
      };

var uploadProgress = DriveService.Files.Create(newFile, fileStream, fileType);
Google.Apis.Upload.IUploadProgress progress =  uploadProgress.Upload();

【问题讨论】:

  • 请问你这个代码 sn-p 从哪里来的?

标签: google-drive-api google-docs-api


【解决方案1】:

我相信在 v3 中您不需要将mimeType 指定为DriveService.Files.Create() 的第三个参数

  • 这与 v2 中的 Files.Insert() 相反,您还需要指定 Convert = true

  • 更改为: var uploadProgress = DriveService.Files.Create(newFile, fileStream);

  • 使用 Google Docs 确保您的原始 mimeType 是 compatible

  • 确保您正确构建fileStream

【讨论】:

  • 感谢您提供的信息 ziganotschka。该文档有点误导,它显示好像在 V3 中支持。 developers.google.com/drive/api/v3/manage-uploads#java_1
  • 您链接的文档包含File file = driveService.files().create(fileMetadata, mediaContent) 行,这相当于我试图解释的内容:它 - 原始 mimeType 进入 mediaContent 并且相应的 Google mimeType 在 fileMetadata 中指定 -因此无需通过将第三个参数传递给Create() 来再次指定它
  • 我一直在尝试 C3 中的解决方案,如下所示。 API 确实需要第三个参数 fileType="application/vnd.google-apps.document" var newFile = new File { Name = title, MimeType = fileType, }; var uploadProgress = DriveService.Files.Create(newFile, fileStream, fileType).Upload();
  • 我的C#代码如下。我尝试在 File mime type 和 Create 方法中提供 text/plain 选项,但总是返回 Bad Request 400 错误。 fileType="application/vnd.google-apps.document" ; var newFile = 新文件 { 名称 = 标题,MimeType = 文件类型,}; var uploadProgress = DriveService.Files.Create(newFile, fileStream, fileType).Upload();
  • 实际上我想要做的是将文件上传到谷歌驱动器(我们已经在现有应用程序中)并能够通过谷歌文档 API 访问相同的文件,以便我们可以利用该功能像“建议”这样的文档,但是当我尝试使用协作 ID 或文件 ID 访问文件时出错。如果有办法通过 Google Docs API 访问使用 Google Drive API(.doc 或 .docx)上传的文件,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多