【发布时间】:2021-02-23 19:17:01
【问题描述】:
我正在尝试下载一些 google doc 文件,但是在它之后我需要使用导出方法转换为 microsoft word mimetype,它可以正常工作,直到它找到一个大小超过 10 mb 的文件,api 文档说这个是导出文档的限制大小,但我真的需要下载这些文件,我的脚本中的所有内容都可以正常工作,除了抛出的错误是
“此文件太大,无法导出。”。详细信息:“此文件太大,无法导出。” 那么,是否有办法避免这种限制或将文档导出到内容文件夹中
编辑:我要下载的文档不是公开的,所以我认为我需要对获取内容的请求进行身份验证
编辑 2:脚本:
SCOPES = ['https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/spreadsheets']
def main():
#----------------------Google drive auth-----------------------------
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
# Call the Drive v3 API
service = build('drive', 'v3', credentials=creds)
sheets_service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = sheets_service.spreadsheets()
# ID of folder that contain the wanted files
query = "'[ID OF THE FOLDER]' in parents"
response = service.files().list(q=query,
spaces='drive',
fields='files(id, name, parents, webViewLink,exportLinks)').execute()
baseURL="https://docs.google.com/document/d/"
for document in response['files']:
downloadURL=baseURL+document["id"]+"/export?format=doc"
r = requests.get(downloadURL)
with open('pathtosabe, 'wb') as f:
f.write(r.content)
main()
【问题讨论】:
-
为了正确理解您的问题,您能否提供您当前的脚本?而且,在您的情况下,您的 Google 文档是公开共享的还是未共享的?
-
好吧,我注意到我的问题是,我尝试下载的文档仅对共享文档的人可见,因此请求未经过身份验证,它只能获取文件名但不能获取内容,我仍然不知道如何解决它。所以我的问题可能会变成“如何使用 gdrive api 的凭据验证我的请求”
-
感谢您的回复。根据您的回复,您的 Google 文档未公开共享,您无法使用 Drive API 检索 Google 文档的内容。我的理解正确吗?关于
how to auth my request with the credentials of gdrive api的新问题,我认为the sample script of the Quickstart for python 可能有用。 -
只是问题在于要导出的文件的大小限制为 10 mb,api 不允许下载大于 10 mb 的文件,而我所有的文件都比这更大,那这就是为什么我尝试使用请求而不是 api,但我认为有一种方法可以使用相同的凭据来验证请求,只是我无法理解 requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html
-
感谢您的回复。你能提供复制你的问题的脚本吗?
标签: python google-sheets google-api google-drive-api