我想从本地电脑读取谷歌驱动器的数据(.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))
在尝试导出xlsx文件时,错误消息Export only supports Docs Editors files
中的export_media
只能用于Google Workspace文件。相反,您可以尝试get_media API。
请参阅https://developers.google.com/drive/api/v3/manage-downloads#python