繁体   English   中英

使用 Google API 创建电子表格时在工作表上设置权限

[英]Put permissions on a sheet when create a spreadsheet with Google API

我在 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)}`);
        }
    }
)

这将返回电子表格的 ID 和更多信息,但我的问题是,正如我搜索的那样,我还没有找到如何授予 email 访问电子表格的权限。

在获取访问令牌时,您必须在代码中添加谷歌驱动范围/权限

const SCOPES = [
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file'
];

然后在您已经编写的代码之后使用以下代码。

// 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM