【发布时间】:2019-10-15 03:20:12
【问题描述】:
我的服务帐户能够毫无问题地读取文件和文件夹。当我尝试使用 fileId 作为父级在现有文件夹中创建新文件夹或文件时,父级总是返回“找不到文件”。我已经在驱动器中查询了可用文件夹,并且出现了我试图用作父文件夹的文件夹。
我使用的范围是“https://www.googleapis.com/auth/drive”,我假设它是全局的。
exports.listDrives = async function listDrives(drive) {
const res = await drive.drives.list()
if (res.data) {
return {success: true, data: res.data}
} else {
return {success: false, message: 'Error listing drives.'}
}
}
exports.listFiles = async function listFiles(drive) {
const res = await drive.files.list({driveId: '0AE58XC8YRuIHUk9PVA', includeItemsFromAllDrives: true, corpora: 'drive', supportsAllDrives: 'true'})
if (res.data) {
return {success: true, data: res.data}
} else {
return {success: false, message: 'Error requesting file list.'}
}
}
exports.createFolder = async function createFolder(drive, projectName) {
const resource = {
name: Date.now().toString(),
mimeType: 'application/vnd.google-apps.folder',
parents: ['1cmEWI_iRQ0R2QHZE1lhbIB9KcbtTts1C']
// parent contained in listed drive
}
const res = await drive.files.create({
resource
}).catch(e => {
console.log('error creating file', e.errors)
})
if (res.data) {
console.log(res.data)
return {success: true, data: res.data}
} else {
return {success: false, message: 'Error creating folder.'}
}
}
Output I'm receiving:
{ kind: 'drive#driveList',
drives:
[ { kind: 'drive#drive',
id: '0AHau-J3SwHpjUk9PVA',
name: 'FlashSparkTest' },
{ kind: 'drive#drive',
id: '0AE58XC8YRuIHUk9PVA',
name: 'TestTwo' } ] }
{ kind: 'drive#fileList',
incompleteSearch: false,
files:
[ { kind: 'drive#file',
id: '1cmEWI_iRQ0R2QHZE1lhbIB9KcbtTts1C',
name: 'test2 testFolder',
mimeType: 'application/vnd.google-apps.folder',
teamDriveId: '0AE58XC8YRuIHUk9PVA',
driveId: '0AE58XC8YRuIHUk9PVA' } ] }
error creating file [ { domain: 'global',
reason: 'notFound',
message: 'File not found: 1cmEWI_iRQ0R2QHZE1lhbIB9KcbtTts1C.',
locationType: 'parameter',
location: 'fileId' } ]
【问题讨论】:
-
在哪里可以看到
1cmEWI_iRQ0R2QHZE1lhbIB9KcbtTts1C的文件夹? -
它出现在我列出文件夹时,也出现在我的驱动器帐户下的 UI 中。列出时,我可以看到我的驱动器帐户中的所有文件夹。我就是不能给他们任何一个写信。相反,会为新文件夹创建一个新的 fileId。
-
感谢您的回复。在您的脚本中,使用了服务帐户。而且您的脚本似乎尝试在您的 Google Drive 中创建该文件夹。我认为你的问题的原因是这个。服务帐户与您的帐户不同。因此,当使用服务帐户时,文件会被放到服务帐户的 Google Drive 中。并且这个驱动器在浏览器中是看不到的。这样,就会发生这样的错误。如果您想在您帐户的 Google Drive 中创建文件夹。请使用 OAuth2。如果这不是您想要的方向,我很抱歉。
-
感谢 Tanaike 的回复,但我需要在后端建立与 Google 的连接,因此需要服务帐户。我阅读的文档指出,只要驱动器或文件夹与服务帐户电子邮件地址共享,服务帐户就应该能够访问、读取和写入共享文件夹/驱动器。现在我能读,但不能写。
-
感谢您的回复。当您帐户的 Google Drive 中的文件夹作为 writer 与服务帐户共享时,可以使用服务帐户在该文件夹中创建新文件夹和文件。那么您能否再次确认该文件夹是否与作为作者的服务帐户共享?也请再次确认文件夹ID。