Google Drive API Python Client: 批量创建文件夹ID列表如何获取?
Google Drive API Python Client: How to retrieve list of created folder IDs when created in batch?
以下代码使用批处理请求创建一组文件夹。
from google.oauth2 import service_account
from googleapiclient import discovery, http
credentials = service_account.Credentials.from_service_account_file('/home/user/Documents/code/credentials.json')
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/drive'])
drive = discovery.build('drive', 'v3', credentials=scoped_credentials)
names = ['test1', 'test2', 'test3']
batch = drive.new_batch_http_request()
for name in names:
folder_metadata = {'name': name,
'mimeType': 'application/vnd.google-apps.folder'}
batch.add(drive.files().create(body=folder_metadata, fields='id'))
res = batch.execute()
但是,我想检索元组列表(folder_name_、folder_id)。
res
返回的对象是 None
.
有什么想法吗?
编辑
这是更新后的代码,但 res
仍然是 None
。
names = ['test1', 'test2', 'test3']
def receive_callback(request_id, response, exception):
#response should have the details about folders
return response['id'], response['name']
# batch = drive.new_batch_http_request()
batch = drive.new_batch_http_request(receive_callback)
for name in names:
folder_metadata = {'name': name,
'mimeType': 'application/vnd.google-apps.folder'}
batch.add(drive.files().create(body=folder_metadata, fields='id, name'))
res = batch.execute()
感谢您的帮助。
最佳,
def receive_callback(request_id, response, exception):
#response should have the details about folders
替换
batch = drive.new_batch_http_request()
与
batch = drive.new_batch_http_request(receive_callback)
您将收到您提出的文件夹创建请求的每个请求的响应
以下代码使用批处理请求创建一组文件夹。
from google.oauth2 import service_account
from googleapiclient import discovery, http
credentials = service_account.Credentials.from_service_account_file('/home/user/Documents/code/credentials.json')
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/drive'])
drive = discovery.build('drive', 'v3', credentials=scoped_credentials)
names = ['test1', 'test2', 'test3']
batch = drive.new_batch_http_request()
for name in names:
folder_metadata = {'name': name,
'mimeType': 'application/vnd.google-apps.folder'}
batch.add(drive.files().create(body=folder_metadata, fields='id'))
res = batch.execute()
但是,我想检索元组列表(folder_name_、folder_id)。
res
返回的对象是 None
.
有什么想法吗?
编辑
这是更新后的代码,但 res
仍然是 None
。
names = ['test1', 'test2', 'test3']
def receive_callback(request_id, response, exception):
#response should have the details about folders
return response['id'], response['name']
# batch = drive.new_batch_http_request()
batch = drive.new_batch_http_request(receive_callback)
for name in names:
folder_metadata = {'name': name,
'mimeType': 'application/vnd.google-apps.folder'}
batch.add(drive.files().create(body=folder_metadata, fields='id, name'))
res = batch.execute()
感谢您的帮助。 最佳,
def receive_callback(request_id, response, exception):
#response should have the details about folders
替换
batch = drive.new_batch_http_request()
与
batch = drive.new_batch_http_request(receive_callback)
您将收到您提出的文件夹创建请求的每个请求的响应