1 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊
我相信你的目標(biāo)如下。
您想從 Drive API 中的“files.get”方法檢索到的縮略圖鏈接中檢索縮略圖。
從示例縮略圖鏈接中,您想從 Google 文檔(文檔、電子表格等)中檢索縮略圖。
問題和解決方法:
目前階段,從縮略圖看的情況似乎404
是bug。這已經(jīng)報(bào)告給了谷歌問題跟蹤器。Ref谷歌方面似乎已經(jīng)為人所知。不幸的是,我認(rèn)為這是當(dāng)前的直接答案。而且,我相信這個(gè)問題會(huì)在未來的更新中得到解決。
在這里,作為當(dāng)前的解決方法,將其轉(zhuǎn)換為 PDF 文件并檢索縮略圖如何?在這種情況下,可以使用縮略圖鏈接。此解決方法的流程如下。
將 Google 文檔轉(zhuǎn)換為 PDF 文件。
PDF 文件創(chuàng)建到與 Google 文檔相同的文件夾中。
從創(chuàng)建的 PDF 文件中檢索縮略圖鏈接。
將上面的流程轉(zhuǎn)換成python腳本后,就變成了下面這樣。
示例腳本:
在使用此腳本之前,請?jiān)O(shè)置訪問令牌和文件 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)
運(yùn)行此腳本時(shí),Google Docs 文件將導(dǎo)出為 PDF 數(shù)據(jù),PDF 數(shù)據(jù)將上傳到 Google Drive 并檢索縮略圖鏈接。
筆記:
在這種情況下,請將范圍包括
https://www.googleapis.com/auth/drive
到您的訪問令牌的范圍內(nèi)。因?yàn)槲募蟼髁恕?/p>為了檢索文件元數(shù)據(jù)和導(dǎo)出 PDF 文件并上傳數(shù)據(jù),需要使用訪問令牌。但是當(dāng)從縮略圖鏈接中檢索到縮略圖時(shí),不需要使用訪問令牌。
2020年1月之后,access token不能與查詢參數(shù)一起使用
access_token=###
,請?jiān)谡埱箢^中使用access token。解決上述問題后,我認(rèn)為您可以使用您的腳本。
添加回答
舉報(bào)