根据Google套件业务计划,我需要从某个用户处获取文件,然后通过域范围授权正式授权的服务帐户将其复制到另一个用户拥有的Team Drive文件夹。
我的方法:考虑到服务帐户有权访问用户数据,更改用户文件的权限以允许整个域读取它,将其复制到另一个<拥有的团队驱动器内的文件夹中/ em> user,然后将Team Drive的所有者设置为复制文件的所有者。
以下代码始终为第一个Google云端硬盘请求返回找不到文件(更改权限)。我保证文件确实存在于用户的文件夹中。
const fs = require('fs');
const path = require('path');
const google = require('googleapis');
fs.readFile('/path/to/my/service/account/credentials', (readError, serviceAccountData) => {
/**
serviceAccountData is an object:
{
"type": "service_account",
"project_id": "__",
"private_key_id": "__",
"private_key": "__",
"client_email": "__@__.iam.gserviceaccount.com",
"client_id": "__",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/___.iam.gserviceaccount.com"
}
*/
if (readError) {
console.log(readError);
} else {
let serviceAccount = null;
try {
serviceAccount = JSON.parse(serviceAccountData);
} catch (parseError) {
console.log(parseError);
return;
}
const jwtClient = new google.auth.JWT(serviceAccount.client_email, null, serviceAccount.private_key, SCOPES, null, 'lordshark@example.com');
jwtClient.authorize((authorizeError, tokens) => {
if (authorizeError) {
console.log(authorizeError);
} else {
// ID of any file that belongs to lordshark@example.com
// This file is stored in lordshark@example.com Google Drive (user space)
let sourceFileId = '____';
// ID of a folder in a Team Drive OWNED BY A DIFFERENT user that isn't lordshark@example.com
// lordshark@example.com DOES NOT HAVE access to this Team Drive
let teamDriveFolderId = '____'/
const googleDrive = google.drive({
version: 'v3',
auth: jwtClient
});
googleDrive.permissions.create({
fileId: sourceFileId,
useDomainAdminAccess: true,
resource: {
role: 'reader',
type: 'domain',
domain: 'example.com',
}
}, (createPermissionError, permissionResponse) => {
if (createPermissionError) {
console.log(createPermissionError);
} else {
googleDrive.files.copy({
fileId: sourceFileId,
supportsTeamDrives: true,
ignoreDefaultVisibility: true,
resource: {
description: 'Copy of a file from user to store in Team Driver',
parents: [teamDriveFolderId]
}
}, (copyError, copyResponse) => {
if (copyError) {
console.log(copyError);
} else {
googleDrive.permissions.create({
fileId: copyResponse.id,
transferOwnership: true,
resource: {
role: 'owner',
type: 'user',
emailAddress: administrator
}
}, (ownerPermissionError, ownerPermissionResponse) => {
if (ownerPermissionError) {
console.log(ownerPermissionError);
} else {
console.log(ownerPermissionResponse);
}
});
}
});
}
});
}
});
}
});
stacktrace:
{错误:找不到档案:1K-dQt5uwfUc331uKc2vxUn3MP3nohLPZ。 在Request._callback(node_modules \ googleapis \ node_modules \ google-auth-library \ lib \ transporters.js:85:15) 在Request.self.callback(node_modules \ request \ request.js:186:22) 在emitTwo(events.js:106:13) 在Request.emit(events.js:191:7) 在请求。 (node_modules \请求\ request.js:1163:10) 在emitOne(events.js:96:13) 在Request.emit(events.js:188:7) 在IncomingMessage。 (node_modules \请求\ request.js:1085:12) 在IncomingMessage.g(events.js:292:16) 在emitNone(events.js:91:20) 代码:404, 错误: [{domain:'global', 理由:'notFound', 消息:'找不到文件:1K-dQt5uwfUc331uKc2vxUn3MP3nohLPZ。', locationType:'参数', location:'fileId'}]}