Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active January 29, 2024 01:17
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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)
@nullberg
Copy link

nullberg commented Jul 7, 2018

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

@prashantg445
Copy link

prashantg445 commented Jul 11, 2018

for access token, visit the following link
https://help.talend.com/reader/Ovc10QFckCdvYbzxTECexA/EoAKa_oFqZFXH0aE0wNbHQ

Sample code:

import requests
headers = {"Authorization": "Bearer ya28.Glv1BYHBki9KOc6Ehy8BpBcwG7xSSOgJXJwGSUFpNYZ0BXGSc_G_CgmvEmlntQTg1rFKXEG21z7IseqJm_BbEcG1Bq1YYOQFxWPoMSKesUA2I6_TK8BYhOZPRCyV"} #put ur access token after the word 'Bearer '
para = {
    "name": "dl.zip", #file name to be uploaded
    "parents": ["10INL0aRBfPx3kz6EMcGrkt5jlNAdbZ2o"] # make a folder on drive in which you want to upload files; then open that folder; the last thing in present url will be folder id
}
files = {
    'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
    'file': ('application/zip',open("./dl.zip", "rb")) # replace 'application/zip' by 'image/png' for png images; similarly 'image/jpeg' (also replace your file name)
}
r = requests.post(
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
    headers=headers,
    files=files
)
print(r.text)

@josiemariahlee
Copy link

josiemariahlee commented Jan 1, 2019

Hi, this has been very helpful, thanks! However, when I tried to upload jpg files, it returned "content type not supported". Any thoughts why? I have succesfully uploaded png files thought. My code:

import json
import requests
headers = {"Authorization": "Bearer MY ACCESS TOKEN"} 
para = {
    "name": "TEST.jpg", 
    "parents": ["MY FOLDER ID"] 
}
files = {
    'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
    'file': ('image/jpeg', open("./TEST.jpg", "rb"))
}
r = requests.post(
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
    headers=headers,
    files=files
)
print(r.text)

@Vlasko
Copy link

Vlasko commented Aug 9, 2019

Is it possible to download files using a similar method? I have tried to tweak the code, but have not had any success

@tanaikech
Copy link
Author

What is a similar method you are saying?

@Vlasko
Copy link

Vlasko commented Aug 10, 2019

Sorry, I mean using the requests.post() method rather than using quickstart

@tanaikech
Copy link
Author

You want to upload files in your local PC to your Google Drive without the library of google-api-python-client for Python. If my understanding is correct, I think that the documents of Files: create and Upload files are useful.

@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