Reputation: 43
I'm trying to upload file on Google Drive using http POST request without Google libs.
So, that's what i've found here:
import json
import requests
headers = {"Authorization": "Bearer " + ACCESS_TOKEN}
para = {
"title": "image_url.jpg",
"parents": [{"id": "root"}, {"id": "### folder ID ###"}]
}
files = {
"data": ("metadata", json.dumps(para), "application/json; charset=UTF-8"),
"file": requests.get("image_url").content
}
response = requests.post("https://www.googleapis.com/upload/drive/v2/files? uploadType=multipart", headers=headers, files=files)
return response
I was trying to adapt it a bit for local files and do something like this:
def upload_file(access_token, filename, filepath, parentID = "root"):
query_link = 'https://www.googleapis.com/upload/drive/v3/files'
query_params = "?uploadType=multipart"
link = '{0}{1}'.format(query_link, query_params)
headers = {"Authorization": "Bearer " + access_token}
meta_data = {
"title": filename,
"parents": [{"id": parentID}]
}
files = {
"data": ("metadata", json.dumps(meta_data), "application/json; charset=UTF-8"),
"file": open(filepath + "\\" + filename, 'rb')
}
response = requests.post(link, headers=headers, files=files)
But all i got is creating Untitled files in my directory, so can you help me to fix this?
Upvotes: 0
Views: 991
Reputation: 43
Found solution. In third version of Google Drive API they use field name instead of title, now everything is working!
Upvotes: 1