0

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

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)
michael.wang
  • 1
  • 1
  • 3
  • Check this related [SO question](http://stackoverflow.com/questions/20942626/filesinsert-google-drive-sdk-python-example-what-is-drive-api-service-ins?rq=1), I think it can give you an idea about POST request – Mr.Rebot Aug 03 '16 at 06:28

1 Answers1

0

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.

s007
  • 728
  • 6
  • 12
  • Right now my code looks like what I posted in UPDATE 1. It just creates a blank json file titled 'untitled'. What am I doing wrong? – michael.wang Aug 17 '16 at 17:51