使用python API在Google驱动器中显示子文件夹 [英] showing subfolders in google drive using python api

查看:63
本文介绍了使用python API在Google驱动器中显示子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试列出Google驱动器中的所有文件夹(和子文件夹).

I'm trying to list all folders(and subfolders) in google drive.

我的根文件夹中有六个子文件夹.但是我的代码只显示文件.

My root folder has six subfolders in it. but my code is only showing files.

def credentials_from_file():
    """Load credentials from a service account file
    Args:
        None
    Returns: service account credential object

    https://developers.google.com/identity/protocols/OAuth2ServiceAccount
    """

    # https://developers.google.com/identity/protocols/googlescopes#drivev3
    SCOPES = [
        'https://www.googleapis.com/auth/drive'
    ]
    SERVICE_ACCOUNT_FILE = './auth_creds.json'

    credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)


    return credentials

credentials = credentials_from_file()
service = discovery.build('drive', 'v3', credentials=credentials)


results = service.files().list(pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])

if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print(u'{0} ({1})'.format(item['name'], item['id']))

如何得知子文件夹?谢谢!

How do I get it to tell me the subfolders as well? Thanks!

更新#1.这是OAuth版本.它允许浏览器创建一个令牌,然后应运行,但是在创建令牌后,它会在执行时冻结:

UPDATE #1. This is teh OAuth version. It allows the browser to create a token, and then should run, but after the token is created, it freezes on execution:

from httplib2 import Http
from oauth2client import file, client, tools
from getfilelistpy import getfilelist

SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'

store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('rrc_crds.json', SCOPES)
    creds = tools.run_flow(flow, store)

resource = {
    "oauth2": creds.authorize(Http()),
    "fields": "files(name,id)",
}
res = getfilelist.GetFileList(resource)  # or r = getfilelist.GetFolderTree(resource)
print(res)

推荐答案

我想提出以下修改.

  • 在脚本中,您正在使用服务帐户.从您的评论中,我可以了解到您想在自己的Google云端硬盘中检索文件.因此,我建议针对这种情况使用OAuth2,因为服务云端硬盘帐户与您使用Google帐户登录的云端硬盘不同.
  • 关于脚本,为了检索特定文件夹下的所有文件和文件夹,我已经为此发布了一个库.所以在这里,我想提出一个建议.
    • In your script, you are using Service Account. From your comment, I could understand that you want to retrieve files in you own Google Drive. So I propose to use OAuth2 for this situation, because the Drive of Service Account is different from your Drive that you login using your Google account.
    • About the script, in order to retrieve all files and folders under a specific folder, I have published a library for this. So here, I would like to propose it.
      • The library is https://github.com/tanaikech/getfilelistpy. This library uses the method of list in Drive API v3.
      • You can install by $ pip install getfilelistpy.

      使用OAuth2的示例脚本如下.在此示例脚本中,OAuth2的过程使用 Google的快速入门.在运行脚本之前,请检查此内容.

      A sample script using OAuth2 is as follows. In this sample script, the process of OAuth2 uses the quickstart of Google. Please check this before you run the script.

      from httplib2 import Http
      from oauth2client import file, client, tools
      from getfilelistpy import getfilelist
      
      SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
      
      store = file.Storage('token.json')
      creds = store.get()
      if not creds or creds.invalid:
          flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
          creds = tools.run_flow(flow, store)
      
      resource = {
          "oauth2": creds.authorize(Http()),
          "id": "### Folder ID ###",
          "fields": "files(name,id)",
      }
      res = getfilelist.GetFileList(resource)  # or r = getfilelist.GetFolderTree(resource)
      print(res)
      

      • 如果您不使用"id":"###文件夹ID ###" ,则会检索自己Google云端硬盘中的所有文件.因此,当驱动器中有很多文件时,将需要很长时间.因此,首先,请使用文件和文件夹较少的文件夹的特定文件夹ID作为测试运行.
        • If you don't use "id": "### Folder ID ###", all files in own Google Drive are retrieved. So when a lot of files in your drive, it will take a long time. So at first, please use the specific folder ID of a folder which has a small number of files and folders as a test run.
        • 这篇关于使用python API在Google驱动器中显示子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆