【发布时间】:2021-01-06 19:27:53
【问题描述】:
我正在从谷歌驱动器下载一些文档,然后我需要在本地处理数据,处理完数据后,我需要获取下载文档的可共享链接并将其附加到电子表格中,其中包含生成的处理数据,我的实际代码来了
import os
import pickle
import os.path
import io
import shutil
from lector_nombre import lectorNombre
from Tablas import lectorTablas
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaIoBaseDownload
SCOPES = ['https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive']
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 there are no (valid) credentials available, let the user log in.
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)
#-----------------------Download the files---------------------
# ID DE LA CARPETA A DESCARGAR
query = "'1qQ3245SwqSAOeqsuBdsh-ZFCqm' in parents"
response = service.files().list(q=query,
spaces='drive',
fields='files(id, name, parents)').execute()
for document in response['files']:
#file_id = service.files.list()
request = service.files().get_media(fileId=document['id'])
fileName = document['name']
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print(document['name'])
print ("Download %d%%." % int(status.progress() * 100))
print("--------------")
fh.seek(0)
with open(fileName, 'wb') as f:
shutil.copyfileobj(fh, f, length=131072)
origen = "./"+document['name']
shutil.move(origen, "./storingDir")
------------------------------------------code to procces the downloaded data--------------------------
[...]
------------------------------------------getting the sharable link-------------------------------
[...]
我正在阅读documentation,但我不明白如何使用 webViewLink 获取链接
【问题讨论】:
-
不是
document['webViewLink']吗? -
你知道不能保证 webViewLink 以后不会改变吧?比如下次你更新文件时说。
-
什么是
some documents?可以问一下他们的 mimeType 吗? -
我刚刚意识到我必须在字段中包含 webViewLink 才能在文档中调用["webviewlink] 非常感谢@RandomDavis