簡體   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