无法使用python通过Google Drive API下载Google电子表格

卡丹布

我正在尝试将电子表格文件从驱动器下载到计算机。我能够进行身份验证,获取文件列表,甚至成功获取元数据。但是,当我尝试下载文件时,出现以下错误:

downloading file starts
An error occurred: <HttpError 400 when requesting https://www.googleapis.com/dri
ve/v2/files/1vJetI_p8YEYiKvPVl0LtXGS5uIAx1eRGUupsXoh7UbI?alt=media returned "The
 specified file does not support the requested alternate representation.">
downloading file ends

我在SO上没有任何此类问题或疑问,SO上提供的用于下载电子表格的其他方法或解决方案已过时。Google已弃用了这些方法或解决方案。

这是代码,我正在使用该文件下载:

import httplib2
import os    
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools


from apiclient import errors
from apiclient import http

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

#SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secrets.json'
APPLICATION_NAME = 'Drive API Quickstart'


def get_credentials():
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'drive-quickstart.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()

    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatability with Python 2.6
            credentials = tools.run(flow, store)
        print 'Storing credentials to ' + credential_path
    return credentials

def main():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v2', http=http)

    file_id = '1vJetI_p8YEYiKvPVl0LtXGS5uIAx1eRGUupsXoh7UbI'

    print "downloading file starts"
    download_file(service, file_id)
    print "downloading file ends "

def download_file(service, file_id):

    local_fd = open("foo.csv", "w+")
    request = service.files().get_media(fileId=file_id)
    media_request = http.MediaIoBaseDownload(local_fd, request)

    while True:
        try:
            download_progress, done = media_request.next_chunk()
        except errors.HttpError, error:
            print 'An error occurred: %s' % error
            return
        if download_progress:
            print 'Download Progress: %d%%' % int(download_progress.progress() * 100)
        if done:
            print 'Download Complete'
            return

if __name__ == '__main__':
    main()
卡丹布

这段代码对我有用。我只需要从Google开发人员仪表板上下载client_secret.json,并将其保存在与python脚本相同的目录中。

在list_of_lists变量中,我得到了一个列表,每一行都是列表。

import gspread
import json
from oauth2client.client import SignedJwtAssertionCredentials


json_key = json.load(open('client_secret.json'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)

gc = gspread.authorize(credentials)
sht1 = gc.open_by_key('<id_of_sheet>')
worksheet_list = sht1.worksheets()
worksheet = sht1.sheet1
list_of_lists = worksheet.get_all_values()

for row in list_of_lists :
    print row

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过Python中的Google Drive API重命名Google电子表格?

使用Drive API访问Google Drive电子表格

使用 PHP API 在 Google Drive 电子表格中对表格进行排序

如何通过Python API使用Google Drive v3 API限制编辑者共享电子表格?

使用 Java 和 Google Drive API 创建一个空电子表格

下载 Google 电子表格 Google API PHP

将本地 Excel.xlsx 文件转换为 Google 电子表格 Google DRIVE API

.Net 如何在 Google Drive 中创建 Google 电子表格?

用户界面和Google Drive / Google电子表格中的选择性同步

使用Python从Google文档下载电子表格

Google Drive Picker ViewId.SPREADSHEET显示电子表格和表单

下载公共Google电子表格

使用Google云端硬盘API从文件夹下载电子表格

使用Google Sheets API公开电子表格?

使用 google 电子表格 api 转义文本

尝试将CSV文件导入Google Drive电子表格时出现400错误的请求

Gsheets Apps 脚本:在 Google Drive 的特定文件夹中制作电子表格的副本

将电子表格文件中的工作表复制到 Google Drive 文件夹

Google电子表格脚本

Google电子表格公式

使用 Drive Picker 将电子表格 ID 保存到模型

从Android上的Google云端硬盘下载Google表格电子表格

如何通过表格API v4更新Google电子表格

Rselenium - 无法在 iframe 中下载电子表格

无法使用Google Sheets API创建新的电子表格

从Python中的链接下载电子表格

使用Python使用Google API将电子表格复制到另一个电子表格

将所有用户别名下载到Google电子表格

Google电子表格直接下载链接(仅一张)为Excel