Python 从Google Drive下载csv文件时遇到问题

Python 从Google Drive下载csv文件时遇到问题,python,google-api,google-drive-api,google-api-python-client,Python,Google Api,Google Drive Api,Google Api Python Client,我想我不明白我做错了什么。我已经连接到Google Drive API,现在我想阅读Google Drive上的一些文档,目的是分析这些csv文件中包含的数据 这是我的代码: from __future__ import print_function import pickle import os.path from httplib2 import Http from googleapiclient.discovery import build from google_auth_oauthlib

我想我不明白我做错了什么。我已经连接到Google Drive API,现在我想阅读Google Drive上的一些文档,目的是分析这些csv文件中包含的数据

这是我的代码:

from __future__ import print_function
import pickle
import os.path
from httplib2 import Http
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/emmanuelsibanda/credentials.json"
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
creds = None
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()
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

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

def gdrive_download(file_id):
  request = service.files().get(fileId=file_id)
  return request.execute()
a = gdrive_download('19sPsANUblhXkb3nZVy4PMrfUyOSoF_uQ')

只是重申一下,我想下载一些csv文件,然后继续阅读这些文件。

在驱动器中,文件的元数据与其内容是分开的。您要求使用
metadata.readonly
scope,毫不奇怪,它只允许访问元数据。调用
files()。获取
然后获取元数据

要获取内容,首先需要选择一个更宽松的范围。然后您将获取内容。这取决于文件是二进制文件还是Google文档(如电子表格)

通过将
alt=media
添加到URL,可以下载二进制文件

可以通过向URL添加
/export?mimeType=text/csv
来导出Google文件


有关详细信息和代码示例,请参阅。

显示错误您现在的代码中有什么不起作用?我想我的困惑在于读取文件中的数据。据我所见,我只有文件的ID、文件名等,但没有文件的实际内容