Google api 驱动器api-未找到写入共享文件夹
我正在使用google drive api写入位于共享驱动器中的文件夹。 我已经创建了一个服务帐户,服务帐户已添加到文件夹中,并具有“内容管理器”权限 然而,当我尝试使用api上载文件时,我不断收到一个错误,说明“未找到文件夹”。 当我尝试在我的个人驱动器上创建一个文件夹并添加具有“编辑器”权限的服务帐户时,同样的方法也可以很好地工作 如果我遗漏了什么或者是按照设计的,有人能帮我吗 下面是示例代码段: 谷歌认证:Google api 驱动器api-未找到写入共享文件夹,google-api,google-drive-api,google-api-client,Google Api,Google Drive Api,Google Api Client,我正在使用google drive api写入位于共享驱动器中的文件夹。 我已经创建了一个服务帐户,服务帐户已添加到文件夹中,并具有“内容管理器”权限 然而,当我尝试使用api上载文件时,我不断收到一个错误,说明“未找到文件夹”。 当我尝试在我的个人驱动器上创建一个文件夹并添加具有“编辑器”权限的服务帐户时,同样的方法也可以很好地工作 如果我遗漏了什么或者是按照设计的,有人能帮我吗 下面是示例代码段: 谷歌认证: const driveauth = new google.auth.JWT(gSu
const driveauth = new google.auth.JWT(gSuiteUser, null,
JSON.parse(gSuiteKey), [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.appdata'
])
const drive = google.drive({
version: 'v3',
auth: driveauth
});
以下是在google drive上上载的代码:
const fileMetadata = {
'name': 'Filename',
'mimeType': 'application/vnd.google-apps.spreadsheet',
parents: [gSuiteFolder],
convert: true
};
const media = {
mimeType: 'text/csv',
body: csvData
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}, (err, file) => {
if (err) {
// Handle error
console.error(`Failure`);
callback(err);
} else {
console.log('Success', file.data.id);
callback(undefined, "done");
}
});
我想你的陪审团可能出了问题
// loads credentials from .env file
require("dotenv").config();
import { google } from "googleapis";
function initializeDrive(version = "v3") {
const client_email = process.env.GOOGLE_CLIENT_EMAIL;
// add some necessary escaping so to avoid errors when parsing the private key.
const private_key = process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n");
// impersonate an account with rights to create team drives
const emailToImpersonate = "some-user@acme-industries.com";
const jwtClient = new google.auth.JWT(
client_email,
null, // undefined if you use TypeScript
private_key,
["https://www.googleapis.com/auth/drive"],
emailToImpersonate
);
return google.drive({
version: version,
auth: jwtClient
});
}
或者更简单一点
const auth = new google.auth.JWT({ // JWT instead of GoogleAuth
subject: "me@mycompany.com", // include this property
keyFile: "service-account.json",
scopes: [ ... ],
})
结果表明,我们需要发送其他属性“supportsAllDrives”,如下所示:
drive.files.create({
resource: fileMetadata,
media: media,
supportsAllDrives: true,
fields: 'id'
}, (err, file) => {
if (err) {
// Handle error
console.error(`Failure`);
callback(err);
} else {
console.log('Success', file.data.id);
callback(undefined, "done");
}
});
您是否与服务帐户共享该文件夹?是的,文件夹与服务帐户共享:)@DaImTo我真的很困惑,这两个文件夹都具有相同的配置w.r.t.服务帐户。唯一的区别是,一个文件夹位于共享驱动器上,具有content manager角色,另一个文件夹位于个人驱动器上,具有editor角色。请添加一些代码。我已使用示例代码@DaImTo更新了此问题,谢谢@DalmTo发现这是因为我需要为团队驱动添加一个属性“supportsAllDrives”。感谢您支持我调试此:)