如何使用 Google Drive API v3 重命名文件?涉及 Electron 和 Node.js。

5

对于v2版本,我们可以通过Google Drive API获得重命名文件的示例。以下是链接: https://developers.google.com/drive/api/v2/reference/files/patch#examples
以下是如何在JavaScript中使用v2重命名文件的方法。

/**
 * Rename a file.
 *
 * @param {String} fileId <span style="font-size: 13px; ">ID of the file to rename.</span><br> * @param {String} newTitle New title for the file.
 */
function renameFile(fileId, newTitle) {
  var body = {'title': newTitle};
  var request = gapi.client.drive.files.patch({
    'fileId': fileId,
    'resource': body
  });
  request.execute(function(resp) {
    console.log('New Title: ' + resp.title);
  });
}

我需要使用Electron和Node.js创建类似于V2示例的函数。目前为止,我已经完成了以下工作:mfm-gdrive

你能在问题中发布你正在使用的代码以及具体哪些部分不起作用吗?@dhanyn10 - ale13
我找不到任何在v3中重命名文件的函数示例。@ale13 - dhanyn10
现在我注意到你想要Node.js的脚本。根据你的问题,我原以为你想要Javascript的脚本。这是我的英语水平不好所致。我深表歉意。我明白我的回答并不适合你的问题。因此,我必须将其删除,因为我不想让其他用户感到困惑。再次为我的英语水平不好深表歉意。 - Tanaike
1个回答

8
如果你想使用Drive API v3 重命名一个文件,你需要使用Files:update请求,像这样:
function renameFile(auth) {
  const drive = google.drive({version: 'v3', auth});
  var body = {'name': 'NEW_NAME'};
  drive.files.update({
    fileId: 'ID_OF_THE_FILE',
    resource: body,
  }, (err, res) => {
    if (err) return console.log('The API returned an error: ' + err);
    else {
      console.log('The name of the file has been updated!');
    }
  });
}

您还可以通过使用Drive API v3参考文档(此处)来模拟“更新”请求。
至于示例,建议您查看Drive API v3 Node.js快速入门指南(此处),然后根据需要进行适当调整。

参考


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接