我无法从本地 PC 读取 google 驱动器的数据 (.xlsx)
I can't read google drive's data(.xlsx) from local PC
我想从本地 PC 读取 google 驱动器的数据 (.xlsx)。
但是返回如下错误,无法正常读取。你知道原因吗?
- 错误
HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/<file_id>/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet&alt=media returned "Export only supports Docs Editors files.". Details: "[{'domain': 'global', 'reason': 'fileNotExportable', 'message': 'Export only supports Docs Editors files.'}]">
- 代码
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
service = build('drive', 'v3', credentials=creds)
file_id = '###'
request = service.files().export_media(fileId=file_id, mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
fh = io.FileIO('test.xlsx', mode='wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
根据错误消息 Export only supports Docs Editors files
,当您尝试导出 xlsx 文件时,export_media
只能用于 Google 工作区文件。相反,您可以尝试 get_media API.
见
https://developers.google.com/drive/api/v3/manage-downloads#python
我想从本地 PC 读取 google 驱动器的数据 (.xlsx)。 但是返回如下错误,无法正常读取。你知道原因吗?
- 错误
HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/<file_id>/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet&alt=media returned "Export only supports Docs Editors files.". Details: "[{'domain': 'global', 'reason': 'fileNotExportable', 'message': 'Export only supports Docs Editors files.'}]">
- 代码
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
service = build('drive', 'v3', credentials=creds)
file_id = '###'
request = service.files().export_media(fileId=file_id, mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
fh = io.FileIO('test.xlsx', mode='wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
根据错误消息 Export only supports Docs Editors files
,当您尝试导出 xlsx 文件时,export_media
只能用于 Google 工作区文件。相反,您可以尝试 get_media API.
见 https://developers.google.com/drive/api/v3/manage-downloads#python