Google Drive API:无法上传某些文件类型

时间:2012-08-10 13:40:41

标签: python google-app-engine google-drive-api

我使用google appengine创建了一个表单和一个简单的服务器,可以将任意文件类型上传到我的google驱动器。该表单无法用于某些文件类型,而只是提供此错误:

HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v1/files?alt=json returned "Unsupported content with type: application/pdf">

不支持pdf文件吗?

执行上传的appengine代码有点像这样:

def upload_to_drive(self, filestruct):
    resource = {
        'title': filestruct.filename,
        'mimeType': filestruct.type,
    }
    resource = self.service.files().insert(
    body=resource,
    media_body=MediaInMemoryUpload(filestruct.value,
                                     filestruct.type),
    ).execute()


def post(self):
      creds = StorageByKeyName(Credentials, my_user_id, 'credentials').get()
      self.service = CreateService('drive', 'v1', creds)
      post_dict = self.request.POST
      for key in post_dict.keys():
          if isinstance(post_dict[key], FieldStorage):#might need to import from cgi
             #upload to drive and return link
             self.upload_to_drive(post_dict[key]) #TODO: there should be error handling here

我已成功将其用于MS Office文档和图像。它也不适用于文本文件并提供此错误:

HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v1/files?alt=json returned "Multipart content has too many non-media parts">

我尝试在资源字典中取消设置'mimeType'值,让google驱动器自动设置它。我还尝试在MediaInMemoryUpload构造函数中取消设置mime类型值。可悲的是,两者都没有奏效。

1 个答案:

答案 0 :(得分:5)

在我看来,您使用旧版本的Python客户端库并参考Drive API v1,而Drive API v2自6月底开始提供。

请尝试更新您的库,并在https://developers.google.com/drive/examples/python检查完整的Python示例。