【问题标题】:Not able to download google spreadsheet by google drive API using python无法使用 python 通过谷歌驱动 API 下载谷歌电子表格
【发布时间】:2015-08-26 13:56:33
【问题描述】:

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

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()

【问题讨论】:

    标签: python google-drive-api google-sheets


    【解决方案1】:

    Google 电子表格没有媒体。相反,他们有 exportLinks。获取文件元数据,然后查看导出链接并选择合适的 URL。

    【讨论】:

    • 我收到以下元数据,但没有导出链接或下载链接 { 'selfLink': 'parentLink': 'iconLink': 'embedLink': 'alternateLink': 'selfLink': 'selfLink': }
    • 粘贴您在 files.get Try-It 中看到的元数据,以便我们查看
    【解决方案2】:

    这段代码对我有用。我只需要从谷歌开发者仪表板下载 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
    

    【讨论】:

      最近更新 更多