Python request for Google Drive

后端 未结 1 490
礼貌的吻别
礼貌的吻别 2021-01-14 18:42

I am trying to use python requests library to send a file to Google Drive api. The only thing I need it to send a multipart request according to google documentation https:/

相关标签:
1条回答
  • 2021-01-14 18:48

    I figured out a way to do it. The problem is Google wants the meta data and the file together.

        def upload_csv(self, file, description):
          self.refresh()
          url = self.url+'?uploadType=multipart&convert=true' +   urllib.urlencode({'key':self.api_key})
    
          headers = { 'Authorization':'Bearer {}'.format(self.access_token) }
    
          class DataDict(dict):
              def read(self):
                  return str( self )
    
          data = ('metadata',DataDict(title = file,description = description),'application/json; charset=UTF-8')
          file = (file,open(file,'rb'),'text/csv')
          files = {'data':data, 'file':file }
          response = requests.post( url, headers = headers, files = files )
          return respone
    
    0 讨论(0)
提交回复
热议问题