Welcome folks today in this blog post we will be uploading files to drive using the google drive v3 in python. All the full source code of the application is shown below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import json import requests headers = { "Authorization":"Bearer ###youraccesstoken###" } para = { "name":"kane.jpg", "parents":[""] } files = { 'data':('metadata',json.dumps(para),'application/json;charset=UTF-8'), 'file':open('./kane.jpg','rb') } r = requests.post("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", headers=headers, files=files ) |
As you can see you need to replace the accessToken
of your google account. This can be captured inside the google oauth2 playground. And the we are declaring the path of the image to be uploaded to google drive. And then we are setting the metadata of the uploaded file such as the name and then actual image file is first of all opened using the open() method and then we are making a POST Request to the google drive api v3 and passing the headers and the actual files which is selected by the user.