Google Drive thumbnailLink返回404

2024-09-28 23:23:32 发布

您现在位置:Python中文网/ 问答频道 /正文

这种获取谷歌硬盘文件缩略图的方法对我来说一直很有效,但最近似乎停止了

我在网上找到的所有答案都表明这是因为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;如建议的{a3}
  • 在浏览器中打开thumbnailLink(以文件所有者身份登录到Google)
  • 在浏览器中打开修改的thumbnailLink-将/u//替换为/u/0//u/1//u/2/(当我以该用户身份打开驱动器时,URL为https://drive.google.com/drive/u/1/my-drive

有人知道获取谷歌硬盘缩略图文件的可靠方法吗


Tags: 文件方法httpscomauthhttpdataresponse
1条回答
网友
1楼 · 发布于 2024-09-28 23:23:32

我相信你的目标如下

  • 您希望从通过驱动器API中的“files.get”方法检索的缩略图链接检索缩略图
  • 从示例缩略图链接中,您希望从Google文档(文档、电子表格等)检索缩略图

问题和解决方法:

在当前阶段,缩略图中的404情况似乎就是错误所在。这已经向谷歌问题追踪者报告Ref而且谷歌方面似乎已经众所周知了。不幸的是,我认为这是目前的直接答案。而且,我相信这个问题会在未来的更新中得到解决

在这里,作为当前的解决方法,如何将其转换为PDF文件并检索缩略图?在这种情况下,可以使用缩略图链接。此解决方案的流程如下所示

  1. 将Google文档转换为PDF文件。
    • PDF文件被创建到Google文档的同一文件夹中
  2. 从创建的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, 'GET', 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, 'GET', 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 Drive并检索缩略图链接

注:

  • 在这种情况下,请将https://www.googleapis.com/auth/drive的作用域包含到您的访问令牌的作用域中。因为文件是上传的
  • 为了检索文件元数据并导出PDF文件并上载数据,需要使用访问令牌。但是,当从缩略图链接检索缩略图时,不需要使用访问令牌
  • 2020年1月之后,访问令牌不能与查询参数access_token=###一起使用。因此,请使用访问令牌访问请求头Ref
  • 当上述问题解决后,我认为您可以使用您的脚本

参考资料:

相关问题 更多 >