【问题标题】:Google sheets with python带有 python 的 Google 工作表
【发布时间】:2020-05-22 18:34:28
【问题描述】:

我有 100 多张 Google 表格与很多人共享。我正在尝试从访问列表中删除不活跃的人。 python有没有办法从版本历史中提取为谷歌表做出贡献的人的列表? 我使用 gspread 库访问工作表,但不知道如何获取贡献用户列表。

from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build

scope = ['https://www.googleapis.com/auth/drive.activity.readonly']
creds = ServiceAccountCredentials.from_json_keyfile_name('accessAPI.json', scopes = scope)

drive_service = build('driveactivity', 'v2', credentials=creds)

edit_activities = drive_service.activity().query(body={"filter":"detail.action_detail_case:EDIT", 
                                                       "itemName":"items/xyz",
                                                       "consolidationStrategy":"legacy"}).execute()

# Call the People API
scope = ['https://www.googleapis.com/auth/contacts.readonly']
creds = ServiceAccountCredentials.from_json_keyfile_name('accessAPI.json', scopes = scope)

service = build('people', 'v1', credentials=creds)
results = service.people().get(resourceName='people/1080745054',personFields='names,emailAddresses').execute()

通过人员 API 运行人员 ID 会返回以下结果。它不包含电子邮件地址

{'resourceName': 'people/1080745054',
 'etag': '%EgcBAj0JPjcuGgQBAgUH'}

输出是否被截断?

【问题讨论】:

    标签: python google-sheets google-apis-explorer gspread google-people-api


    【解决方案1】:

    方法

    使用 Python,您可以通过 Drive API 和 People API 实现此行为。

    1. 使用 Drive Activities API 为您的 google 表格获取 EDIT 活动
    2. 从 Drive Activities API 响应正文中的 actors 对象资源获取编辑人员 ID。
    3. 使用编辑人员 ID 从 People API 获取编辑电子邮件地址。
    4. 使用 Drive API Permissions 资源端点列出您 google 表格上的所有文件权限。
    5. 如果用户不在编辑器列表中,请根据您的逻辑更新权限。

    这是建议的伪代码脚本:

    loop your_google_sheets:
        edit_activities = drive_service.activites().query(filter="detail.action_detail_case:EDIT", itemName="items/"+your_google_sheets.id)
        editors = edit_activities.get(actors_ids)
        loop editors:
            editors_emails += people_service.people().get(resourceName=editors.personName, personFields="emailAddresses")
        file_permissions = drive_service.permissions().list(fileId=your_google_sheets.id)
        loop file_permissions:
            update_if_not_editor(editors_email, file_permissions.id) # Implement your own logic
    

    参考文献

    People API get

    Drive Activities API query

    Drive API Permissions list

    Drive API Permissions update

    【讨论】:

    • 感谢您的回复。 activity().query 给了我 HttpError。我已经更新了问题描述中的代码。
    • Drive Activity API 使用与 Drive API 不同的范围。使用"https://www.googleapis.com/auth/drive.activity.readonly" 而不是"https://www.googleapis.com/auth/drive"
    • 谢谢,成功了。现在我被困在 people-api 响应中。输出不包含电子邮件地址。我已经更新了描述。再次感谢!
    • 始终查看文档“试用”功能中执行操作所需的范围。显然范围contacts.readonly 不足以执行此操作。您应该能够使其与 userinfo 范围一起使用。
    • “试试看”给了我详细信息,但在 python 中没有。我确实尝试了不同的范围,但没有任何帮助。它没有给我“名称”键。我想知道这是由于帐户不公开。这可能是原因吗? [链接] (stackoverflow.com/questions/50766097/…)
    猜你喜欢
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多