gpt4 book ai didi

python - 尽管添加了正确的范围,但带有 Python 的 Google Drive API 仍不允许文件下载

转载 作者:行者123 更新时间:2023-12-01 00:17:32 26 4
gpt4 key购买 nike

我已关注quickstart Google 给出的使用 Python 的方法,我已使用 Google 给出的适当范围从云端硬盘下载文件 https://www.googleapis.com/auth/drive.readonly但我不断收到以下错误:

googleapiclient.errors.HttpError: https://www.googleapis.com/drive/v3/files/1RWpLGCWldcJyVqa0tIVlScg60ExEtcNIvJ7R9M8DuhM?alt=media returned "Only files with binary content can be downloaded. Use Export with Google Docs files."

当我尝试运行代码来下载文件时。

我可以读取驱动器上的文件,但尽管我尽了最大努力,但似乎无法从驱动器下载特定的电子表格。下面是我的代码(经过编辑的文件路径和一些注释),用于通过 API 建立连接:

def gsuite_connect():

file_path = 'OMITTED/Loading'

# Get what permissions the user (using the API) will need. This as been set to high level
# access by default
scopes = ['https://www.googleapis.com/auth/drive.readonly']

# Access the tokens for G Suite to access the Drive. Ensure that if this file previous exists,
# that it is in the current working directory
store = file.Storage(os.path.join(file_path, 'storage.json'))

# Access the credentials for the Drive API
creds = store.get()

if not creds or creds.invalid:
print("\nUsing credentials found in client_id(secret).json")
flow = client.flow_from_clientsecrets(os.path.join(file_path, 'client_id.json'), scopes)
creds = tools.run_flow(flow, store)

http = creds.authorize(Http())
drive = discovery.build('drive', 'v3', http=http)
sheets = discovery.build('sheets', 'v4', http=http)

return drive, sheets

这是我用来下载基于 Google provides 的文件的函数(已编辑的文件路径和一些注释) :

    def get_datalog(self):

dir_path = 'OMITTED/Downloads'
fname = "'FILENAME'"
files = self.drive.files().list(q="name = {}".format(fname),
fields="nextPageToken, files(id, name)").execute()
items = files.get('files', [])

# Error checking and subsequent downloading if file successfully found
if not items:
exit()
else:

# Change into the desired directory for storing the file and download file based on the
# retrieved ID
os.chdir(dir_path)
file_id = items[0]['id']

# Request download service
request = self.drive.files().get_media(fileId=file_id)

fh = io.FileIO(fname, mode='w')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))

# Return the file path
return os.path.join(dir_path, fname)

帮助将不胜感激!我不想显示敏感文件,例如 client_id.json 或任何其他凭据,但如果您需要更多信息,请告诉我!

最佳答案

  • 您想要使用 google-api-python-client 和 python 下载 Google 文档(在您的情况下,它是电子表格)。
  • 您想知道只能下载包含二进制内容的文件的错误原因。使用导出 Google 文档文件。
  • 您已经能够使用 Drive API。

如果我的理解是正确的,这个答案怎么样?

修改点:

  • 当通过get_media方法下载Google Docs文件时,会出现此类错误。
    • 如果使用 get_media 方法,则可以下载除 Google 文档之外的文件(电子表格、文档、幻灯片等)。
  • 当您想要下载 Google 文档文件时,请使用 export_media 方法。
    • 在这种情况下,由于 Google 方面的规范,原始 Google 文档无法下载。所以请将其转换为其他格式。例如,对于电子表格,它是 Excel 格式、CSV 格式等。

修改后的脚本:

为了避免这个问题,下面的修改如何?

从:
request = self.drive.files().get_media(fileId=file_id)
到:
request = self.drive.files().export_media(fileId=file_id, mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
  • 在这种情况下,Google 电子表格会下载为 Excel 文件。如果您想以 CSV 格式下载,请将 mimeType 修改为 text/csv

注意:

  • 在本例中,它假定您要下载的 Google 文档文件是您自己的或公开共享的。

引用文献:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

添加:

关于更改访问 token 范围的方法,请重命名或删除脚本中的storage.json文件,然后再次运行脚本。通过此操作,您可以对新范围进行重新授权,并创建包含 token 的新文件。您可以将访问 token 与新范围一起使用。

关于python - 尽管添加了正确的范围,但带有 Python 的 Google Drive API 仍不允许文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59212443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com