我需要以编程方式修改我的Google Drive,就创建文件夹和上传一堆文件的能力而言,然后,当需要时-删除根文件夹并重新执行整个过程。
我已经创建了一个具有服务帐户的项目,然后下载了JSON并将其存储在我的计算机上。
接下来,我关注了this tutorial。
我最终得到了这段代码:
const auth = await google.auth.getClient({
credentials: require(pathToServiceAccountJSON),
scopes: "https://www.googleapis.com/auth/drive"
});
const drive = await google.drive({ version: "v3", auth });
drive.files
.create({
resource: {
name: filename,
mimeType: "application/vnd.google-apps.folder",
parents: [parentId]
}
})
.then(result => console.log("SUCCESS:", result))
.catch(console.error);
但是,执行它会导致抛出以下错误:
{
...
errors: [{
domain: "global",
reason: "forbidden",
message: "Forbidden"
}]
}
发布于 2019-12-25 04:45:29
首先,如果你迷路了,这个quick start from Google可能比教程更好。
其次,要访问你的驱动器,你必须在你的应用程序中请求适当的作用域,并且你必须通过访问授权过程中提供的URL来授权应用程序所请求的权限(作用域)。Here is a guide to scopes。
发布于 2019-12-25 10:32:46
为了能够使用服务帐户模拟用户(如您自己或域中的任何其他用户),您需要启用全域委托,为此,您需要有一个G套件帐户1。如果是这种情况,在库示例2中,您需要在构造JWT对象时将您想要模拟的用户添加为第5个参数:
const {JWT} = require('google-auth-library');
const keys = require('./jwt.keys.json');
async function main() {
const client = new JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/cloud-platform'],
'userToImpersonate@example.com'
);
const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
const res = await client.request({url});
console.log(res.data);
}
如果您没有G套件帐户,您可以简单地按照快速入门3个步骤获取驱动器服务,然后使用它进行创建请求。
https://stackoverflow.com/questions/59471961
复制