问题描述
这种获取Google云端硬盘文件缩略图的方法对我来说一直有效,但最近似乎已停止。
我在网上可以找到所有答案,这表明这是因为thumbnailLink需要授权(eg)。但是,我正在使用授权的访问令牌访问缩略图。我可以使用带有这些访问令牌的Drive API "Files: get"获取文件信息,但是thumbnailLink返回404。
print(http)
# <google_auth_httplib2.AuthorizedHttp object at 0x11561d0f0>
# An instance of google_auth_httplib2.AuthorizedHttp
url = 'https://www.googleapis.com/drive/v3/files/%s?fields=thumbnailLink' % file_id
response,content = http.request(url)
data = json.loads(content)
print(data['thumbnailLink'])
# https://docs.google.com/u//Feeds/vt?gd=true&id=***fileID***&v=203&s=***&sz=s220
# Works ✓
response,content = http.request(data['thumbnailLink'])
print(response['status'])
# 404
# :(
也给出了404错误:
-
thumbnailLink + "&access_token=" + YOURTOKEN;
,如建议的here。 - 在浏览器中打开
thumbnailLink
(以文件所有者身份登录到Google)。 - 在浏览器中打开修改后的
thumbnailLink
-用/u//
,/u/0/
和/u/1/
替换/u/2/
(当我以该用户身份打开驱动器时,URL为https://drive.google.com/drive/u/1/my-drive)
解决方法
我相信您的目标如下。
- 您要从通过Drive API中“ files.get”方法检索的缩略图链接中检索缩略图。
- 您想通过示例缩略图链接从Google文档(文档,电子表格等)中检索缩略图。
问题和解决方法:
在当前阶段,缩略图中404
的情况似乎是错误。这已被报告给Google问题跟踪器。 Ref看来Google方面已经广为人知。不幸的是,我认为这是当前的直接答案。而且,我相信以后的更新将解决此问题。
在这里,作为当前的解决方法,如何将其转换为PDF文件并检索缩略图?在这种情况下,可以使用缩略图链接。此解决方法的流程如下。
- 将Google文档转换为PDF文件。
- 将PDF文件创建到Google文档的同一文件夹中。
- 从创建的PDF文件中检索缩略图链接。
当以上流程转换为python脚本时,它如下所示。
示例脚本:
在使用此脚本之前,请设置访问令牌和文件ID。在这种情况下,为了使用简单的脚本请求multipart/form-data
,我使用了requests
库。
import json
import httplib2
import requests
import time
http = httplib2.Http()
access_token = '###' # Please set the access token.
file_id = '###' # Please set the file ID.
headers = {"Authorization": "Bearer " + access_token}
# 1. Retrieve filename and parent ID.
url1 = "https://www.googleapis.com/drive/v3/files/" + file_id + "?fields=*"
res,res1 = http.request(url1,'GET',headers=headers)
d = json.loads(res1.decode('utf-8'))
# 2. Retrieve PDF data by converting from the Google Docs.
url2 = "https://www.googleapis.com/drive/v3/files/" + file_id + "/export?mimeType=application%2Fpdf"
res,res2 = http.request(url2,headers=headers)
# 3. Upload PDF data as a file to the same folder of Google Docs.
para = {'name': d['name'] + '.pdf','parents': d['parents']}
files = {
'data': ('metadata',json.dumps(para),'application/json; charset=UTF-8'),'file': res2
}
res3 = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",headers=headers,files=files
)
obj = res3.json()
# It seems that this is required to use by creating the thumbnail link from the uploaded file.
time.sleep(5)
# 4. Retrieve thumbnail link of the uploaded PDF file.
url3 = "https://www.googleapis.com/drive/v3/files/" + obj['id'] + "?fields=thumbnailLink"
res,res4 = http.request(url3,headers=headers)
data = json.loads(res4.decode('utf-8')) # or data = json.loads(res4)
print(data['thumbnailLink'])
# 5. Retrieve thumbnail.
response,content = http.request(data['thumbnailLink'])
print(response['status'])
print(content)
- 运行此脚本时,会将Google Docs文件导出为PDF数据,并将PDF数据上传到Google云端硬盘并检索缩略图链接。
注意:
- 在这种情况下,请将
https://www.googleapis.com/auth/drive
的范围包括在访问令牌的范围内。因为文件已上传。 - 为了检索文件元数据并导出PDF文件并上载数据,需要使用访问令牌。但是,从缩略图链接中检索缩略图时,不需要使用访问令牌。
- 2020年1月之后,访问令牌不能与查询参数
access_token=###
一起使用。因此,请将访问令牌用于请求标头。 Ref - 上述问题解决后,我认为您可以使用您的脚本。