python - 使用 gspread 创建的电子表格正在与我的组织中的所有用户共享

标签 python google-drive-api gspread

我有一个 Python 脚本,可以解析 HTML 文件并将我需要的信息放入由服务帐户创建并与我共享的 Google 表格文档中。一切正常,除了生成的服务帐户拥有的电子表格不仅与我共享,而且与我的 G Suite 组织中的每个人共享。这没什么大不了的,因为我的组织很小,但我确信我的同事不希望这些文件弄乱他们的“与我共享”文件夹。

我已经在网上搜索了这个问题并仔细阅读了 Stack Overflow,但还没有想出任何尝试更改我的代码的想法。我还想知道这是否是我需要在 Google API 控制台中更改的设置,但也没有找到任何内容。

相关代码如下:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

def loadTemplate(template_name, output_name):
        # use creds to create a client to interact with the Google Drive API

        print("Connecting to Google API...")

        scope = ['https://www.googleapis.com/auth/drive']

        creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
        client = gspread.authorize(creds)

        # Make a copy of the template and name it something that uniquely identifies this report

        print("Copying template...")

        open_spreadsheet = client.open(template_name)
        copy_spreadsheet = client.copy(open_spreadsheet.id,title=output_name, copy_permissions=True)
        copy_spreadsheet.share('myname@myorganization.com', perm_type='user', role='writer', notify=False)

        return copy_spreadsheet

template_spreadsheet = loadTemplate(template_name, output_name)

同样,一切都按预期运行,只是结果与我的 G Suite 组织中的所有用户共享,而不仅仅是与我 (myname@myorganization.com) 共享。如有任何建议,我们将不胜感激!

最佳答案

我明白了,但我觉得自己很愚蠢......

我正在复制模板,并设置copy_permissions=True。该模板实际上并未与我的整个组织共享,但它与我碰巧检查过的两个人共享,因此默认情况下它的每个副本都与相同的两个人共享。更改为 copy_permissions=False 解决了该问题。

关于python - 使用 gspread 创建的电子表格正在与我的组织中的所有用户共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57664442/

相关文章:

java - 如何在本地存储 Google Drive SDK 的凭据

java - 如何使用 Java 查找 Google Drive 中的 isFolderExist 和 isFileExist?

java - Google Drive API 无法列出文件 (v3 Java)

python - 属性错误: module 'gspread' has no attribute 'service_account'

python - 使用pandas在谷歌电子表格中添加新行和现有行

python - 用 Python 解方程。计算第n位数字

python - 如何获取文件系统上文件的内容 'filter' ?

python - 将 jpg 转换为灰度

一段时间后Python Gspread 401错误

Python 将 'True' 和 'False' 解析为字符串