python - Google Drive API POST requests? -


i'm trying interact google drive api , while example working, i'd learn how make post requests in python instead of using pre-written methods. example, in python how make post request insert file? insert file

how add requests , parameters body?

thanks!

update 1:

        headers = {'content-type': 'application/json', 'authorization': 'bearer ' + 'my auth token'}         datax = {'name': 'upload.xlsx', 'parents[]': ['0bymnveruzwxmwdnkref1cwhwczq']}         r = requests.post('https://www.googleapis.com/upload/drive/v3/files/', headers=headers, data=json.dumps(datax))         response = json.loads(r.text)         fileid = response['id']         headers2 = {'authorization': 'bearer ' + 'my auth token'}         r2 = requests.patch('https://www.googleapis.com/upload/drive/v3/files/' + fileid + '?uploadtype=media', headers=headers2) 

to insert file:

  1. create file in google drive , id in response
  2. insert file using id

here post parameters both operations:

url: 'https://www.googleapis.com/drive/v3/files' headers: 'authorization bearer <token>' content-type: application/json body: {  "name": "temp", "mimetype": "<mime type of file>" } 

in python can use "requests"

import requests import json headers = {'content-type': 'application/json','authorization': 'bearer <your oauth token' } data = {'name': 'testing', 'mimetype': 'application/vnd.google-apps.document'} r  = requests.post(url,headers=headers,data=json.dumps(data)) r.text 

above post response give id. insert in file use patch request following parameters

url: 'https://www.googleapis.com/upload/drive/v3/files/'<id of file created> '?uploadtype=media' headers: 'authorization bearer <token>' content-type: <mime type of file created> body:  <your text input> 

i hope can convert in python requests.


Comments