【发布时间】:2020-10-07 21:18:41
【问题描述】:
我正在尝试使用 python 和 google Drive API 从共享驱动器中的文件中获取 thumbnailLink,但是,文件信息不包括 thumbnailLink(尽管对于大多数文件,hasThumbnail 字段,我确实将其作为字段对于文件,值为 true)
我环顾四周,发现的解决方案似乎都不起作用(尽管这是我的第一个 python 项目以及我的第一个 google drive api 项目,所以我可能只是不知道我在做什么)
我尝试过的:
- 将范围设置为“https://www.googleapis.com/auth/drive”(之前是 ..drive.metadata.readonly)
- 使用通配符:results = drive.files().list(pageSize=10, fields="*",blablabla...)。例如,如果我尝试 fields="thumbnailLink" 它找不到任何文件。
- 获得列表后,我尝试使用该列表中每个文件的 ID 来执行 file = service.files().get(fileId=item_id, supportsAllDrives=True, fields="*").execute() 但同样的情况发生了,我有许多字段,包括设置为 true 的 hasThumbnail 字段,但没有缩略图链接。
- 我尝试使用官方网站上的“Try this API”控制台,实际上我确实获得了thumbnailLink! (具有与上述相同的参数)。所以我不明白为什么在我的应用程序请求时缺少它。
编辑(代码): 我有这样一种方法
SCOPES = ['https://www.googleapis.com/auth/drive']
def getDrive():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
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=53209)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
return service
然后我从这里调用它并获取文件:
def getFiles(request):
drive = getDrive()
# Call the Drive v3 API
results = drive.files().list(
pageSize=10, fields="*", driveId="blabla", includeItemsFromAllDrives=True, corpora="drive", supportsAllDrives=True).execute()
items = results.get('files', [])
getItems = []
for item in items:
item_id = item['id']
getItems.append(drive.files().get(fileId=item_id, supportsAllDrives=True, fields="*").execute())
if not items:
print('No files found.')
else:
print('Files:')
print(getItems)
for item in items:
# print(u'{0} ({1})'.format(item['name'], item['id']))
print(item)
return render(request, "index.html", {'files': getItems})
另外,是的,我确实使用服务帐户,我可以检索我需要的所有文件,而不是缩略图链接。
我认为调用 list() 然后调用 get() 是没有意义的,但我读到可以通过 get() 方法解决问题,但在我的情况下它不起作用。
【问题讨论】:
-
我能问你的问题吗? 1. 请问您要检索缩略图链接的文件的mimeType 吗? 2. 我可以查看您当前的脚本来复制您的问题吗?
-
你能提供你的完整代码吗?您使用的是服务帐号吗?
-
您好,感谢您的快速回复!我已经更新了上面的问题,如果您还有其他问题,请告诉我
标签: python google-drive-api thumbnails