6 回答

TA貢獻1853條經驗 獲得超6個贊
要使用范圍“https://www.googleapis.com/auth/drive”,您需要提交谷歌應用程序進行驗證。
因此,使用范圍“https://www.googleapis.com/auth/drive.file”而不是“https://www.googleapis.com/auth/drive”來上傳文件而不進行驗證。
也使用 SCOPES 作為列表。
前任:SCOPES = ['https://www.googleapis.com/auth/drive.file']
我可以使用上面的 SCOPE 成功地將文件上傳和下載到谷歌驅動器。

TA貢獻1786條經驗 獲得超13個贊
“權限不足:請求的身份驗證范圍不足?!?/p>
意味著您已通過身份驗證的用戶尚未授予您的應用程序執行您嘗試執行的操作的權限。
files.create方法要求您已使用以下范圍之一對用戶進行身份驗證。
而您的代碼似乎確實使用了完整的驅動范圍。我懷疑發生的事情是您已經對用戶進行了身份驗證,然后更改了代碼中的范圍,并且沒有促使用戶再次登錄并同意。您需要從您的應用程序中刪除用戶的同意,方法是讓他們直接在他們的谷歌帳戶中刪除它,或者只是刪除您存儲在應用程序中的憑據。這將強制用戶再次登錄。
谷歌登錄還有一個批準提示強制選項,但我不是 python 開發人員,所以我不完全確定如何強制。它應該類似于下面的 prompt='consent' 行。
flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope='https://spreadsheets.google.com/feeds '+
'https://docs.google.com/feeds',
redirect_uri='http://example.com/auth_return',
prompt='consent')
同意屏幕
如果操作正確,用戶應該會看到這樣的屏幕
提示他們授予您對其云端硬盤帳戶的完全訪問權限
令牌泡菜
如果您在https://developers.google.com/drive/api/v3/quickstart/python遵循谷歌教程,則需要刪除包含用戶存儲同意的 token.pickle。
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)

TA貢獻1852條經驗 獲得超1個贊
您可以使用google-api-python-client構建Drive 服務以使用Drive API。
使用有效的Drive Service,您可以通過調用如下函數來上傳文件upload_file
:
def upload_file(drive_service, filename, mimetype, upload_filename, resumable=True, chunksize=262144):
media = MediaFileUpload(filename, mimetype=mimetype, resumable=resumable, chunksize=chunksize)
# Add all the writable properties you want the file to have in the body!
body = {"name": upload_filename}
request = drive_service.files().create(body=body, media_body=media).execute()
if getFileByteSize(filename) > chunksize:
response = None
while response is None:
chunk = request.next_chunk()
if chunk:
status, response = chunk
if status:
print("Uploaded %d%%." % int(status.progress() * 100))
print("Upload Complete!")
現在傳入參數并調用函數...
# Upload file
upload_file(drive_service, 'my_local_image.png', 'image/png', 'my_imageination.png' )
您將在 Google Drive 根文件夾中看到名為my_imageination.png的文件。
有關 Drive API v3 服務和可用方法的更多信息,請點擊此處。
getFileSize()功能:
def getFileByteSize(filename):
# Get file size in python
from os import stat
file_stats = stat(filename)
print('File Size in Bytes is {}'.format(file_stats.st_size))
return file_stats.st_size
上傳到驅動器中的某些文件夾很容易...
只需在請求正文中添加父文件夾 ID。
這是File 的屬性。
例子:
request_body = {
"name": "getting_creative_now.png",
"parents": ['myFiRsTPaRentFolderId',
'MyOtherParentId',
'IcanTgetEnoughParentsId'],
}

TA貢獻1785條經驗 獲得超4個贊
回答:
刪除您的token.pickle
文件并重新運行您的應用程序。
更多信息:
只要您擁有正確的憑據集,那么在更新應用程序范圍時所需要做的就是重新獲取令牌。刪除位于應用程序根文件夾中的令牌文件,然后再次運行應用程序。如果你有https://www.googleapis.com/auth/drive
范圍,并且在開發者控制臺中啟用了 Gmail API,你應該很好。

TA貢獻1839條經驗 獲得超15個贊
也許這個問題有點過時了,但我找到了一種從 python 上傳文件到谷歌驅動器上的簡單方法
pip install gdrive-python
然后,您必須允許腳本使用此命令在您的 Google 帳戶上上傳文件并按照說明操作:
python -m drive about
最后,上傳文件:
form gdrive import GDrive
drive = GDrive()
drive.upload('path/to/file')
有關 GitHub 存儲庫的更多信息:https ://github.com/vittoriopippi/gdrive-python

TA貢獻1813條經驗 獲得超2個贊
我找到了將文件上傳到谷歌驅動器的解決方案。這里是:
import requests
import json
url = "https://www.googleapis.com/oauth2/v4/token"
payload = "{\n\"" \
"client_id\": \"CLIENT_ID" \
"\",\n\"" \
"client_secret\": \"CLIENT SECRET" \
"\",\n\"" \
"refresh_token\": \"REFRESH TOKEN" \
"\",\n\"" \
"grant_type\": \"refresh_token\"\n" \
"}"
headers = {
'grant_type': 'authorization_code',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
res = json.loads(response.text.encode('utf8'))
headers = {
"Authorization": "Bearer %s" % res['access_token']
}
para = {
"name": "file_path",
"parents": "google_drive_folder_id"
}
files = {
'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
# 'file': open("./gb.png", "rb")
'file': ('mimeType', open("file_path", "rb"))
}
r = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
print(r.text)
要生成客戶端 ID、客戶端密碼和刷新令牌,您可以點擊鏈接:-單擊此處
添加回答
舉報