我想弄清楚如何使用Google的Drive API在Team drive中创建文件。
这里是google drive api文档中对文件和teamdrives的引用。
https://developers.google.com/drive/api/v3/reference/files
https://developers.google.com/drive/api/v3/reference/files/create
https://developers.google.com/drive/api/v3/enable-shareddrives
const resource = {
name: fileName,
supportsAllDrives: true,
driveId: TEAMDRIVE_ID,
mimeType: 'application/vnd.google-apps.spreadsheet'
};
drive.files.create(
{
resource,
fields: 'id, name, webViewLink'
},
(err, file) => {
if (err) {
return({ msg: 'Failed Creating the file', err });
} else {
return file.data;
}
}
);
该代码能够创建文件,但是不会出现在团队驱动器中。它出现在我的个人驱动器中。我能够读取团队驱动器中的文件。创建文件一直是我的问题...
答案 0 :(得分:0)
我最终找到了问题的答案,这是对我有用的。
const resource = {
name: fileName,
driveId: TEAMDRIVE_ID,
mimeType: 'application/vnd.google-apps.spreadsheet',
parents: [parent_id]
};
drive.files.create(
{
resource,
fields: 'id, name, webViewLink'
supportsAllDrives: true,
},
(err, file) => {
if (err) {
return({ msg: 'Failed Creating the file', err });
} else {
return file.data;
}
}
);
我不得不将supportsAllDrives: true
从资源对象中移出,并将其作为选项移至drive.files.create
参数中。
还需要将parents
添加到resource
对象。 parent_id可以是Team Drive ID或Team Drive中的文件夹ID。