谷歌驱动API-JavaScript插入文件-创建本机谷歌文档

谷歌驱动API-JavaScript插入文件-创建本机谷歌文档,javascript,google-drive-api,Javascript,Google Drive Api,为了创建(插入)一个新文件,我一直在使用JavaScript Google Drive API。它工作得很好,只是它创建的文档不是google文档,而是某种可以转换为google文档的文档 我想要一个真正的谷歌文档。我尝试了“convert:true”,但它似乎无法将其转换为文档 insertFile: function (filename, content, callback) { const boundary = '-------314159265358979323846'; con

为了创建(插入)一个新文件,我一直在使用JavaScript Google Drive API。它工作得很好,只是它创建的文档不是google文档,而是某种可以转换为google文档的文档

我想要一个真正的谷歌文档。我尝试了“convert:true”,但它似乎无法将其转换为文档

insertFile: function (filename, content, callback) {
  const boundary = '-------314159265358979323846';
  const delimiter = "\r\n--" + boundary + "\r\n";
  const close_delim = "\r\n--" + boundary + "--";

  {
    var contentType = 'application/vnd.google-apps.document';
    var metadata = {
      'title': filename,
      'mimeType': 'text/html',
      'description': 'Created'
    };

    // TODO: replace this with a library - https://github.com/beatgammit/base64-js/blob/master/lib/b64.js
    var base64Data = window.btoa(unescape(encodeURIComponent(content)));
    var multipartRequestBody =
        delimiter +
        'Content-Type: application/json\r\n\r\n' +
        JSON.stringify(metadata) +
        delimiter +
        'Content-Type: ' + contentType + '\r\n' +
        'Content-Transfer-Encoding: base64\r\n' +
        '\r\n' +
        base64Data +
        close_delim;

    var request = gapi.client.request({
      'path': '/upload/drive/v2/files',
      'method': 'POST',
      'params': {'uploadType': 'multipart', 'convert': true },
      'headers': {
        'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
      },
      'body': multipartRequestBody
    });
    if (!callback) {
      callback = function (file) {
        console.log(file)
      };
    }
    request.execute(callback);
  }
}
插入文件后,当我转到链接时,我得到:


我想我发现了你的错误。尝试迁移到
v3
,根据:

其他更改

现在,通过在资源正文中设置适当的目标mimeType,而不是指定?convert=true,可以请求导入到Google Docs格式

如果格式不受支持,导入操作将返回400错误

这里是链接