2

我需要将查询传递给函数。应该很简单!这是代码:

def get_file_ID(q):
  dump = drive_service.files().list(q=q, fields = 'items(mimeType,id,title,downloadUrl)').execute()
  fileItems = dump['items']
  for item in fileItems:
    try:
        if item['mimeType'] == "application/octet-stream":
          return item['id']
    except KeyError:
        print "No item of the required type, or item has been deleted"
        return None

def lets_go_get(file_Id):
  f = drive_service.files().get(fileId=file_Id).execute()
  resp, content = drive_service._http.request(f.get('id'))
  if resp.status == 200:
      #print 'Status: %s' % resp
      return content
  else:
      print 'An error occurred: %s' % resp
      return None

text = raw_input('Enter title search text: ')
query = "title contains '" + text +"'"
selectedFile = "'" + get_file_ID(query) +"'"

print lets_go_get(selectedFile)

我收到以下错误:

   HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v2/files/%270BxDfMkL6x0wjMS03Nz.....Tk%27?alt=json returned "File not found: '0BxDfMkL6x0wjMS03.....MTU5NjQ0ZjdhOTk'">

但是,当我用 DIRECT fileID 代替对“lets_go_get”的最后一次调用时,如下所示:

print lets_go_get('0BxDfMkL6x0wjMS03.....MTU5NjQ0ZjdhOTk')

然后它工作。

我看不出两者有什么区别。谁能解释我错过了什么???

非常感谢。

4

1 回答 1

2

用于downloadUrl下载内容:

f = drive_service.files().get(fileId=file_Id).execute()
resp, content = drive_service._http.request(f.get('downloadUrl'))

文档在“Python”选项卡上有一个工作示例:https ://developers.google.com/drive/v2/reference/files/get

于 2013-04-19T11:54:34.877 回答