Node.js 使用google drive.files.copy时设置新文件名

Node.js 使用google drive.files.copy时设置新文件名,node.js,google-drive-api,Node.js,Google Drive Api,我正在使用google drive apiv3将电子表格复制到google drive上的文件夹中。文档说明应使用“name”属性设置新工作表的标题。我正在使用它,但新创建的图纸名为“副本{titleOfSheetToCopy}” 请求: app.get('/new', function (req, res) { var drive = google.drive({version:'v3', auth: jwt_client}); var copyRequest = {

我正在使用google drive apiv3将电子表格复制到google drive上的文件夹中。文档说明应使用“name”属性设置新工作表的标题。我正在使用它,但新创建的图纸名为“副本{titleOfSheetToCopy}”

请求:

app.get('/new', function (req, res) 
{
    var drive = google.drive({version:'v3', auth: jwt_client});

    var copyRequest = {
        name: 'im a copy',
        fileId: 'idOfSheetToClone',
        parents: ['idOfDestinationFolder'],
    }

    drive.files.copy(copyRequest,  function(err, response){
        if(err){
            console.log(err);
            res.send('error');
            return;
        }
        res.send(response);
    });
});
答复:

{
"config": {
    "url": "https://www.googleapis.com/drive/v3/files/-----/copy?name=im%20a%20copy&parents=---",
    "method": "POST",
    "headers": {
        "Accept-Encoding": "gzip",
        "User-Agent": "google-api-nodejs-client/0.7.2 (gzip)",
        "Authorization": "---",
        "Accept": "application/json"
    },
    "params": {
        "name": "im a copy",
        "parents": [
            "---"
        ]
    },
    "responseType": "json"
},
"data": {
    "kind": "drive#file",
    "id": "---",
    "name": "Copy of _template",
    "mimeType": "application/vnd.google-apps.spreadsheet"
},
"headers": {
    "alt-svc": "quic=\":443\"; ma=2592000; v=\"46,43\",h3-Q050=\":443\"; ma=2592000,h3-Q049=\":443\"; ma=2592000,h3-Q048=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000",
    "cache-control": "no-cache, no-store, max-age=0, must-revalidate",
    "connection": "close",
    "content-encoding": "gzip",
    "content-type": "application/json; charset=UTF-8",
    "date": "Mon, 25 Nov 2019 05:37:01 GMT",
    "expires": "Mon, 01 Jan 1990 00:00:00 GMT",
    "pragma": "no-cache",
    "server": "GSE",
    "transfer-encoding": "chunked",
    "vary": "Origin, X-Origin",
    "x-content-type-options": "nosniff",
    "x-frame-options": "SAMEORIGIN",
    "x-xss-protection": "1; mode=block"
},
"status": 200,
"statusText": "OK"
}
可以看到response.data.name显示默认命名,而不是“我是副本”

任何指示都将不胜感激。谢谢

  • 您希望使用Files:copy in Drive API v3的方法复制文件
  • 您希望通过Node.js使用googleapis实现这一点
  • 您已经能够使用驱动器API
如果我的理解是正确的,那么这个答案呢

修改脚本: 在此修改中,
copyRequest
drive.files.copy()
修改如下

var copyRequest = {  // Modified
  name: "im a copy",
  parents: ["idOfDestinationFolder"]
};

drive.files.copy(
  {  // Modified
    fileId: "idOfSheetToClone",
    requestBody: copyRequest  // or resource: copyRequest
  },
  function(err, response) {
    if (err) {
      console.log(err);
      res.send("error");
      return;
    }
    res.send(response);
  }
);
注:
  • 如果出现错误,请使用最新版本的googleapis
参考:

如果我误解了您的问题,并且这不是您想要的结果,我很抱歉。

您必须将新文件名作为
资源
属性的
名称
属性传递。文档中说,
name
属性将作为
requestBody
属性传递,但在我的例子中,这不起作用

解决方案:

drive.files.copy(
  {
    fileId: "idOfSheetToClone",
    resource: { name: "im a copy"}
  },
  function(err, response) {
    if (err) {
      console.log(err);
      res.send("error");
      return;
    }
    res.send(response);
  }
);

这就解决了问题,你的假设是正确的。我看到name属性应该在请求主体中指定。Thanks@justin谢谢你的回复。我很高兴你的问题解决了。“我也谢谢你。”塔奈克为此非常感谢你——我正在拔头发。你知道GoogleNodeJSAPI的一个好的参考站点吗?这里的一切都指向样板文件,我没有发现任何关于(比如)drive.file的所有API及其参数的详细信息。上面给出的参考仅显示RESTAPI,而不是NodeJAPI@Jabberwock谢谢你的评论。当我使用各种语言使用GoogleAPI时,我总是检查每种语言的googleapis存储库。例如,当我使用Node.js使用Google API时,我会检查。我通过检查源脚本来确认如何使用GoogleAPI。因为谷歌API的官方库经常更新,我想检查一下最新的脚本。此外,我们还可以在GitHub上看到关于图书馆的讨论。如果这对你的情况没有帮助,我道歉。