Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active January 29, 2024 01:17
Show Gist options
  • Save tanaikech/8cdfd23807372657dc63d81e25e35153 to your computer and use it in GitHub Desktop.
Save tanaikech/8cdfd23807372657dc63d81e25e35153 to your computer and use it in GitHub Desktop.
Uploading Files From Local To Google Drive by Python without Quickstart

Uploading Files From Local To Google Drive by Python without Quickstart

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 sample :

curl -X POST \
    -H "Authorization: Bearer ### access token ###" \
    -F "metadata={name : 'sample.png', parents: ['### folder ID ###']};type=application/json;charset=UTF-8" \
    -F "file=@sample.png;type=image/png" \
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"

]

Python sample :

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)
@shekhar419
Copy link

Can you tell me the how to download a file from google drive using access token. i tried from google api documentation.
Here is the my code.

access_token = "fZr4I-vk4zuhXLJ98YIc6tpVijqRlVWSoFEGLM"
headers = {"Authorization": "Bearer " + access_token}
file_id = "H92CmUVJ_1n3NureyrEhFk5WmXxH"
r = requests.get("https://www.googleapis.com/drive/v3/files/file_id?alt=media",
headers = headers)
print(r,"***********************8")

but when i run this code it gives me response 200. but where the file is downloaded. i do not get any downloaded file.
please reply me if you have any solution for this.

Thanks

@GautamBothra
Copy link

Hello Shekhar419,

I want to ask, did you get any solution for downloading google drive file in local system.Because i am facing forbidden issue while using the script. Can you help me on this please.

@DiyRex
Copy link

DiyRex commented May 27, 2021

How can I upload folders to google drive insted of files

@yogeshsinghgit
Copy link

Can any one help me to solve this error..

`import json
import requests
headers = {"Authorization": "Bearer MY Access Token"}
para = {
"name": "sample.png",
}
files = {
'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
'file': open("./testing.png", "rb")
}
r = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
print(r.text)

Error :

ValueError: too many values to unpack (expected 2)`

The Code is same as above but I'm getting this error..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment