无论如何,是否有使用Google Sheet API或Google Drive API等在节点JS中删除Google Sheet的访问权限?

发布于 2025-02-02 02:35:14 字数 1441 浏览 3 评论 0 原文

我正在使用Google表API以及Google Drive API(Node JS),并且我通过使用服务帐户创建了Google表格,并与一封电子邮件共享“ [email  procearted] “扮演'reader'的角色。但是现在5分钟后,我想从“ [电子邮件  prection] “,这样一定不能可以访问' >“。
注意:Google Sheet的所有者是服务帐户。 以下是代码的片段。

const auth = new google.auth.GoogleAuth({
    keyFile: "private_keys.json", //the key file
    //url to spreadsheets API
    scopes: ["https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/drive"], 
});

var body = {
  'type': 'user',
  'role': 'reader',
  'emailAddress': '[email protected]',
};

var drive=google.drive({version: 'v3',auth});

drive.permissions.create({
  'fileId': spId,  //sheetID returned from create sheet response
  'resource': body,
}, function(err, response) {if (err) {
  console.error('error-------', err);
  return;
  } else{
    console.log(JSON.parse(JSON.stringify(response))) ;
  }
});

I am using google sheets api as well as google drive api (node js) and I have created google sheet by using service account and shared with one email "[email protected]" with role of 'reader'. but now after 5 minutes, I want to remove the access from "[email protected]", so that it must not be accessible to '[email protected]".
Note: the owner of google sheet is service account.
Below is the snippet of code.

const auth = new google.auth.GoogleAuth({
    keyFile: "private_keys.json", //the key file
    //url to spreadsheets API
    scopes: ["https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/drive"], 
});

var body = {
  'type': 'user',
  'role': 'reader',
  'emailAddress': '[email protected]',
};

var drive=google.drive({version: 'v3',auth});

drive.permissions.create({
  'fileId': spId,  //sheetID returned from create sheet response
  'resource': body,
}, function(err, response) {if (err) {
  console.error('error-------', err);
  return;
  } else{
    console.log(JSON.parse(JSON.stringify(response))) ;
  }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢你一世 2025-02-09 02:35:14

我相信您的目标如下。

在这种情况下,以下示例脚本怎么样?

示例脚本:

const spId = "###"; // Please set the file ID.
const email = "[email protected]"; // Please set the email address you want to delete the permission.

drive.permissions.list(
  { fileId: spId, fields: "permissions(emailAddress,id)" },
  function (err, response) {
    if (err) {
      console.error("error-------", err);
      return;
    } else {
      var permission = response.data.permissions.find(({ emailAddress }) => emailAddress == email);
      if (permission) {
        drive.permissions.delete({ fileId: spId, permissionId: permission.id },
          function (err, response) {
            if (err) {
              console.error("error-------", err);
              return;
            } else {
              console.log(JSON.parse(JSON.stringify(response)));
            }
          });
      }
    }
  });
  • 运行此脚本时,使用电子邮件地址搜索了权限ID。并且,当找到许可时,删除了许可。

参考:

I believe your goal is as follows.

  • You want to delete the permission of [email protected] from a file using googleapis for Node.js.

In this case, how about the following sample script?

Sample script:

const spId = "###"; // Please set the file ID.
const email = "[email protected]"; // Please set the email address you want to delete the permission.

drive.permissions.list(
  { fileId: spId, fields: "permissions(emailAddress,id)" },
  function (err, response) {
    if (err) {
      console.error("error-------", err);
      return;
    } else {
      var permission = response.data.permissions.find(({ emailAddress }) => emailAddress == email);
      if (permission) {
        drive.permissions.delete({ fileId: spId, permissionId: permission.id },
          function (err, response) {
            if (err) {
              console.error("error-------", err);
              return;
            } else {
              console.log(JSON.parse(JSON.stringify(response)));
            }
          });
      }
    }
  });
  • When this script is run, the permission ID is searched using the email address. And, when the permission is found, the permission is deleted.

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文