1 回答
TA貢獻1851條經驗 獲得超5個贊
問題和解決方法:
使用的時候resumable=True,好像0不能使用byte的數據。所以在這種情況下,需要上傳空數據而不使用resumable=True.?但是當我看到PyDrive的腳本時,似乎它被resumable=True用作默認值。參考所以在這種情況下,作為一種解決方法,我想建議使用該requests模塊。訪問令牌是從gauthPyDrive 檢索的。
當你的腳本被修改后,它會變成如下所示。
修改后的腳本:
import io
import requests
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file = drive.CreateFile({'title': 'test.txt'})
file.Upload()
file.SetContentString('hello')
file.Upload()
# file.SetContentString()
# file.Upload()? ? # This throws an exception.
# I added below script.
res = requests.patch(
? ? "https://www.googleapis.com/upload/drive/v3/files/" + file['id'] + "?uploadType=multipart",
? ? headers={"Authorization": "Bearer " + gauth.credentials.token_response['access_token']},
? ? files={
? ? ? ? 'data': ('metadata', '{}', 'application/json'),
? ? ? ? 'file': io.BytesIO()
? ? }
)
print(res.text)
添加回答
舉報
