从Google Drive下载Python文件 403权限

问题描述 投票:0回答:1

为了从我的驱动器下载一个.mp4文件,开始使用Google Drive API与目标CLI工具,需要创建新的证书。

然后,当下载属于一个用户的文件,只发送给我的链接使用谷歌驱动器API。

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import io
from googleapiclient.http import MediaIoBaseDownload

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.file']

def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('drive', 'v3', credentials=creds)

    # Call the Drive v3 API to download the file
    file_id = '1ZVFrwYaLClNrIk6TkshJZamHSS-59LLR'
    request = service.files().get_media(fileId=file_id)
    filename = "test.mp4"
    #fh = io.BytesIO()
    fh = io.FileIO(filename, 'wb') # From - https://stackoverflow.com/a/40309675/5675325
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download %d%%." % int(status.progress() * 100))

if __name__ == '__main__':
    main()

我得到了以下错误信息

  File "C:\Users\tiago\Anaconda3\lib\site-packages\googleapiclient\http.py", line 749, in next_chunk
    raise HttpError(resp, content, uri=self._uri)

HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/1ZVFrwYaLClNrIk6TkshJZamHSS-59LLR?alt=media returned "The user has not granted the app 682654872192 read access to the file 1ZVFrwYaLClNrIk6TkshJZamHSS-59LLR.">

这并不是什么新鲜事(尽管它是 可能是一个误导性的错误信息),是 在其文件中描述的 作为

解决一个403错误。用户没有授予应用程序{appId}{verb}访问文件{fileId}的权限。

其中规定

当您的应用程序不在文件的 ACL 中时,会发生 appNotAuthorizedToFile 错误。

...

要修复此错误,请执行以下操作之一。

您还可以检查文件上的isAppAuthorized字段,查看该文件是否由您的应用程序创建或打开。

如何翻阅并下载文件?

python python-3.x google-drive-api mp4
1个回答
0
投票

为了解决这个问题,我刚刚通过以下两个步骤。

  1. 改变范围为 drive - 所谓 此处此处.

    SCOPES = ['https://www.googleapis.com/auth/drive']
    
  2. 删除 token.pickle,并重新运行脚本 - 如建议的那样。此处.

然后,管理下载文件,它的工作正常。

File downloaded from Google Drive

.mp4 downloaded from Drive

© www.soinside.com 2019 - 2024. All rights reserved.