简体   繁体   English

无法使用 Google Drive 获取共享文件夹中的项目列表 API

[英]Not getting list of items in shared folders with Google Drive API

I have tried to get the list of folders with Google Drive API in Python. I can get the list in my drive folders but I can not get the list of items of shared folders.我试图在 Python 中获取 Google Drive API 的文件夹列表。我可以在我的驱动器文件夹中获取该列表,但我无法获取共享文件夹的项目列表。 I use GCP service account for credential and I already set the service account into shared folder.我使用 GCP 服务帐户作为凭据,并且我已经将服务帐户设置到共享文件夹中。 Anyone has solutions??谁有解决办法?? Should I use another parameter??我应该使用另一个参数吗? Thank you so much.太感谢了。

drive = build('drive', 'v3', credentials=credentials)

folderId = "xxxxxxxxxxx"

query = f"parents = '{folderId}'"
response = drive.files().list(q=query,includeItemsFromAllDrives=True,supportsAllDrives=True).execute()

files = response.get('files')
nextPageToken = response.get('nextPageToken')

while nextPageToken:
    response = drive.files().list(q=query,includeItemsFromAllDrives=True,supportsAllDrives=True).execute()
    files.extend(response.get('files'))
    nextPageToken = response.get('nextPageToken')

df = pd.DataFrame(files)

Your issue is the way you are calling parents你的问题是你打电话给父母的方式

query = f"parents = '{folderId}'"

Parents = should be parents in父母=应该父母

query = f"parents in '{folderId}'"

background info in the event you have the wrong file id for the folder.如果文件夹的文件 ID 错误,请提供背景信息。

Start simple when you are working with the Q parameter.当您使用 Q 参数时,从简单开始。 Can you find the folder by searching for the mimetype?你能通过搜索 mimetype 找到文件夹吗?

page_token = None
while True:
    response = drive_service.files().list(q="mimeType='application/vnd.google-apps.folder'",
                                          
                                          fields='nextPageToken, files(id, name)',
                                          pageToken=page_token).execute()
    for file in response.get('files', []):
        # Process change
        print 'Found file: %s (%s)' % (file.get('name'), file.get('id'))
    page_token = response.get('nextPageToken', None)
    if page_token is None:
        break

Parents in父母在

Assuming that you get the folder back in the previous call you need to take the file id of that call and send it to this call.假设您在上一次通话中取回文件夹,您需要获取该通话的文件 ID 并将其发送到此次通话。

page_token = None
while True:
    response = drive_service.files().list(q="parents in 'FILEIDFROMPREVIOUSCALL'",
                                          
                                          fields='nextPageToken, files(id, name)',
                                          pageToken=page_token).execute()
    for file in response.get('files', []):
        # Process change
        print 'Found file: %s (%s)' % (file.get('name'), file.get('id'))
    page_token = response.get('nextPageToken', None)
    if page_token is None:
        break

I have tried to get the list of folders with Google Drive API in Python.我试图在 Python 中使用 Google Drive API 获取文件夹列表。 I can get the list in my drive folders but I can not get the list of items of shared folders.我可以在我的驱动器文件夹中获取列表,但我无法获取共享文件夹的项目列表。 I use GCP service account for credential and I already set the service account into shared folder.我使用 GCP 服务帐户作为凭据,并且我已经将服务帐户设置到共享文件夹中。 Anyone has solutions??谁有解决方案?? Should I use another parameter??我应该使用另一个参数吗? Thank you so much.太感谢了。

drive = build('drive', 'v3', credentials=credentials)

folderId = "xxxxxxxxxxx"

query = f"parents = '{folderId}'"
response = drive.files().list(q=query,includeItemsFromAllDrives=True,supportsAllDrives=True).execute()

files = response.get('files')
nextPageToken = response.get('nextPageToken')

while nextPageToken:
    response = drive.files().list(q=query,includeItemsFromAllDrives=True,supportsAllDrives=True).execute()
    files.extend(response.get('files'))
    nextPageToken = response.get('nextPageToken')

df = pd.DataFrame(files)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM