在python中通过api上传到Google Drive后如何获取链接?

这里使用带有python的google drive API将文件上传到google drive,我如何获取上传文件的链接。


我试过print (res['exportLinks']),但它给了我这个错误。


KeyError: 'exportLinks'


完整的python代码


#!/usr/bin/env python


from __future__ import print_function

import os


from apiclient import discovery

from httplib2 import Http

from oauth2client import file, client, tools


SCOPES = 'https://www.googleapis.com/auth/drive'

store = file.Storage('storage.json')

creds = store.get()

if not creds or creds.invalid:

    flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)

    creds = tools.run_flow(flow, store)

DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))


FILES = (('hello.txt', False),)


for filename, convert in FILES:

    metadata = {'title': filename}

    res = DRIVE.files().insert(convert=convert, body=metadata,

            media_body=filename, fields='mimeType,exportLinks').execute()

    if res:

        print (res['exportLinks'])

有没有其他方法可以做到这一点。


牧羊人nacy
浏览 186回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python