This is a sample script for uploading files from local PC to Google Drive using Python. In this sample, Quickstart is not used. So when you use this script, please retrieve access token.
curl -X POST \
-H "Authorization: Bearer ### access token ###" \
-F "metadata={name : 'sample.png', parents: ['### folder ID ###']};type=application/json;charset=UTF-8" \
-F "[email protected];type=image/png" \
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
]
When above curl sample is converted to Python, it becomes as follows.
import json
import requests
headers = {"Authorization": "Bearer ### access token ###"}
para = {
"name": "sample.png",
"parents": ["### folder ID ###"]
}
files = {
'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
'file': open("./sample.png", "rb")
}
r = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
print(r.text)
Hello,
I was wondering if someone could provide a simple explanation for how to use this Python code.
I understand that one can obtain the folder ID from the URL (drive.google.com/drive/folders/<folder_ID>).
However, when I use my Client ID for the "Bearer ### access token ###", I get this error:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
Should one not use the Client ID? Should one use a different access token and how does one obtain that access token?
Thanks,
/Nathan