1

This method of getting Google Drive file thumbnails has been working for me but seems to have stopped recently.

All answers I can find online indicate that this is because thumbnailLink requires authorization (eg). However, I'm am accessing the thumbnails with authorized access tokens. I can get the file info using the Drive API "Files: get" with these access tokens but the thumbnailLink returns 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
# :(

Also giving a 404 error:

  • thumbnailLink + "&access_token=" + YOURTOKEN; as suggested here.
  • Opening thumbnailLink in a browser (logged in to Google as the file owner).
  • Opening a modified thumbnailLink in a browser - replacing /u// with /u/0/, /u/1/ , /u/2/ (When I open drive as this user the URL is https://drive.google.com/drive/u/1/my-drive)

Does anyone know a reliable way to get Google Drive thumbnail image files?

Jake
  • 12,713
  • 18
  • 66
  • 96
  • Its not an issue with the fields i tested it myself its working. What is the full error message? You should consider following the official example. https://developers.google.com/drive/api/v3/quickstart/python – Linda Lawton - DaImTo Aug 11 '20 at 06:39

1 Answers1

1

I believe your goal as follows.

  • You want to retrieve the thumbnail from the thumbnail link retrieved by the method of "files.get" in Drive API.
  • From your sample thumbnail link, you want to retrieve the thumbnail from Google Docs (Document, Spreadsheet, and so on).

Issue and workaround:

In the current stage, it seems that the situation of 404 from the thumbnail is the bug. This has already been reported to the Google issue tracker. Ref And it seems that Google side has already been known. Unfortunately, I think that this is the current direct answer. And also, I believe that this issue will be resolved by the future update.

Here, as the current workaround, how about converting it to PDF file and retrieve the thumbnail? In this case, the thumbnail link can be used. The flow of this workaround is as follows.

  1. Convert Google Docs to a PDF file.
    • The PDF file is created to the same folder of the Google Docs.
  2. Retrieve the thumbnail link from the created PDF file.

When above flow is converted to the python script, it becomes as follows.

Sample script:

Before you use this script, please set the access token and file ID. In this case, in order to request multipart/form-data with the simple script, I used requests library.

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)
  • When you run this script, the Google Docs file is exported as the PDF data, and the PDF data is uploaded to Google Drive and retrieve the thumbnail link.

Note:

  • In this case, please include the scope of https://www.googleapis.com/auth/drive to the scopes of your access token. Because the file is uploaded.
  • In order to retrieve the file metadata and export the PDF file and upload the data, the access token is required to be used. But when the thumbnail is retrieved from the thumbnail link, the access token is not required to be used.
  • After January, 2020, the access token cannot be used with the query parameter of access_token=###.So please use the access token to the request header. Ref
  • When above issue was resolved, I think that you can use your script.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165