[英]Put permissions on a sheet when create a spreadsheet with Google API
I am using Google's Spreadsheets API in NodeJS and I know how I can create a Spreadsheet with the following code我在 NodeJS 中使用 Google 的电子表格 API,我知道如何使用以下代码创建电子表格
const auth = new GoogleAuth({
keyFile: 'src/credentials.json',
scopes: "https://www.googleapis.com/auth/spreadsheets"
})
const client = await auth.getClient()
const googleSheets = google.sheets({version:"v4", auth: client})
let resource = {
properties: {
title: 'Test'
},
}
googleSheets.spreadsheets.create(
{
resource,
fields: 'spreadsheetId'
},
(err, spreadsheet) =>{
if (err) {
console.log(err);
} else {
console.log(`${JSON.stringify(spreadsheet)}`);
}
}
)
This returns the ID of the Spreadsheet and more information, but my problem is that as much as I have searched, I have not found how to give permissions to an email to access the Spreadsheet.这将返回电子表格的 ID 和更多信息,但我的问题是,正如我搜索的那样,我还没有找到如何授予 email 访问电子表格的权限。
You will have to add google drive scope/permission in code while getting access token在获取访问令牌时,您必须在代码中添加谷歌驱动范围/权限
const SCOPES = [
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file'
];
and then use following code after the code that you've already written.然后在您已经编写的代码之后使用以下代码。
// PERMISSIONS
const spreadsheetId = spreadsheet.data.spreadsheetId;
drive = await google.drive({ version: "v3", auth: client });
const res = await drive.permissions.create({
resource: {
type: "user",
role: "commenter",
emailAddress: "johnDoe@gmail.com", // Please set the email address you want to give the permission.
},
fileId: spreadsheetId,
fields: "id",
});
// END PERMISSIONS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.