Google Drive API POST requests?
Question: I'm trying to interact with the Google Drive API and while their example is
working, I'd like to learn how to make the POST requests in python instead of
using their pre-written methods. For example, in python how would I make the
post request to insert a file? [Insert a
File](https://developers.google.com/drive/v2/reference/files/insert)

How do I add requests and parameters to the 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)
    


Answer: To insert a file:

  1. Create a file in Google drive and get its _Id_ in response
  2. Insert a file using _Id_

Here are the POST parameters for 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 you 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 will give you an id. To insert in file use _PATCH_ request
with 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 you can convert it in python requests.

