【问题标题】:Python download file from Google Drive 403 permission来自 Google Drive 403 权限的 Python 下载文件
【发布时间】:2020-06-15 16:55:20
【问题描述】:

为了从我的 Drive 下载 .mp4 文件,开始使用 Google Drive API 和目标 CLI 工具,该工具需要创建新凭据。

然后,当下载属于仅使用 Google Drive 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.">

这不是什么新鲜事(尽管它是might be a misleading error message),而是described in their documentation

解决 403 错误:用户尚未授予应用 {appId} {verb} 访问文件 {fileId}

声明

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

...

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

您还可以检查文件上的 isAppAuthorized 字段以查看 文件由您的应用创建或使用您的应用打开。

如何检查并下载文件?

【问题讨论】:

    标签: python python-3.x download google-drive-api mp4


    【解决方案1】:

    为了解决它,我刚刚经历了以下两个步骤。

    1. 将范围更改为 drive - 按照建议的 herehere

      SCOPES = ['https://www.googleapis.com/auth/drive']
      
    2. 删除 token.pickle 并再次运行脚本 - 按照建议 here

    然后,设法下载文件,它工作正常。

    【讨论】:

      猜你喜欢
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2019-12-13
      相关资源
      最近更新 更多