【问题标题】:Google Drive thumbnailLink returns 404Google 云端硬盘缩略图链接返回 404
【发布时间】:2020-08-11 04:43:08
【问题描述】:

这种获取 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 + "&amp;access_token=" + YOURTOKEN; 建议 here
  • 在浏览器中打开thumbnailLink(以文件所有者身份登录 Google)。
  • 在浏览器中打开修改后的thumbnailLink - 将/u// 替换为/u/0//u/1//u/2/(当我以该用户身份打开驱动器时,URL 为https://drive.google.com/drive/u/1/my-drive

有人知道获取 Google Drive 缩略图文件的可靠方法吗?

【问题讨论】:

标签: python google-drive-api


【解决方案1】:

我相信你的目标如下。

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

问题和解决方法:

目前阶段,从缩略图看404的情况似乎是bug。这已报告给 Google 问题跟踪器。 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
  • 当上述问题解决后,我认为您可以使用您的脚本了。

参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多