如何使用google-api-python-client在drive.permissions.list中分页

问题描述 投票:0回答:2

我的问题

我有一个 GDrive 文件夹,我想列出有权访问它的用户。不幸的是,API 不会返回超过 100 个用户,并且记录的分页解决方案不起作用。

(!)也许这与共享驱动器上的测试有关 - 不幸的是,目前我无法在非共享驱动器文件夹上重现。

到目前为止我尝试过的:

我尝试了谷歌文档中描述的分页解决方案,例如我试过:

file_id = "12abcde" # my folder ID request = service.permissions().list( fileId=file_id, fields="permissions(type,emailAddress)", supportsAllDrives=True ) while request is not None: activities_doc = request.execute() request = service.permissions().list_next(request, activities_doc)
循环中的请求始终为 None。

初始请求返回的正文也与文档中描述的

不匹配

有人有制作这项工作的经验吗?

google-drive-api gcloud google-api-python-client
2个回答
1
投票
要获取完整列表,您需要使用

nextPageToken

 并将其放置在 
pageToken
 参数下。

您可以通过将其添加到请求的字段来从响应正文中获取

nextPageToken

您可以使用以下示例代码作为参考:

try: service = build('drive', 'v3', credentials=creds) file_id = "asdfasdfas" # my folder ID permissions_list = [] first_request = service.permissions().list( fileId=file_id, pageSize=100, supportsAllDrives=True, fields="nextPageToken, permissions(type,emailAddress)" ).execute() permissions_list.append(first_request) next_Page_Token = first_request.get('nextPageToken') while next_Page_Token: request = service.permissions().list( pageToken = next_Page_Token, fileId=file_id, pageSize=100, supportsAllDrives=True, fields="nextPageToken, permissions(type,emailAddress)" ).execute() permissions_list.append(request) next_Page_Token = request.get('nextPageToken') except HttpError as error: # TODO(developer) - Handle errors from drive API. print(f'An error occurred: {error}')
更新:

这是一个使用文档中

list_next()

基础的示例
这里

try: service = build('drive', 'v3', credentials=creds) permission_list = [] token = 'test' fileID='folderID' request_made = service.permissions().list( fileId=fileID, pageSize=100, fields="nextPageToken, permissions(type,emailAddress)", supportsAllDrives=True ) request = request_made.execute() permission_list.append(request) while token: nextpage = service.permissions().list_next( previous_request=request_made, previous_response=request) request = nextpage.execute() token = request.get('nextPageToken') permission_list.append(request) request_made = nextpage except HttpError as error: # TODO(developer) - Handle errors from drive API. print(f'An error occurred: {error}')
    

0
投票
我还没有使用过 Drive API,但在相当现代的 Python 实现中(例如 3.11),我很确定

Giselle代码可以这样简化:

try: service = build('drive', 'v3', credentials=creds) permissions_api = service.permissions() # permissions in Giselle's code permissions = [] # permission_list in Giselle's code request = permissions_api.list( fileId='folderID', fields='nextPageToken, permissions(type,emailAddress)', supportsAllDrives=True) while request: response = request.execute() permissions += response # permissions += response.get('permissions', []) maybe this, actually? except HttpError as error: logging.warning('whoopsie!', exc_info=True) # TODO pprint.pp(permissions) # Here they all are
您实际上可能想将 

response

 的子映射附加到 
permission_list
 ,就像我注释掉的行中一样(我不确定结构),但是不需要弄乱分页标记你的代码。默认的 
pageSize
 也应该没问题。

© www.soinside.com 2019 - 2024. All rights reserved.