0

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?

1 Answer 1

1

Found solution. In third version of Google Drive API they use field name instead of title, now everything is working!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.