问题
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!
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)
回答1:
I would like to propose the following modification.
Modification points:
- 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
.
Sample script:
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)
- 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.
References:
- Drive API
- getfilelistpy of python library
来源:https://stackoverflow.com/questions/55009747/showing-subfolders-in-google-drive-using-python-api