Oh Snap!

Please turnoff your ad blocking mode for viewing your site content

Hit enter after type your search item

Python Upload Files to Google Drive Using PyDrive Library Full Tutorial

/
/
/
346 Views

Welcome folks I am back with another blog post. In this post I will be talking about how to upload files to google drive using pydrive library in python full tutorial for beginners.There is a step by step video which is shown below.

 

 

Requirements

 

1) PyDrive Library

 

For building this application we will be using this pydrive library. For installing this library we will be using the pip package manager to install this library. To install this we will be executing this command as follows as shown below

 

pip install pydrive

 

READ  How to Upload Files to Node.js Express Server Using Express FileUpload Library

Authentication

 

Google Drive API requires authentication to make requests to their api. So we need to get the credentials such as client id and client secret. We need to set up in the developer console.

Go to APIs Console and make your own project.

Search for ‘Google Drive API’, select the entry, and click ‘Enable’.

Select ‘Credentials’ from the left menu, click ‘Create Credentials’, select ‘OAuth client ID’.

Now, the product name and consent screen need to be set -> click ‘Configure consent screen’ and follow the instructions. Once finished:

  1. Select ‘Application type’ to be Web application.
  2. Enter an appropriate name.
  3. Input http://localhost:8080 for ‘Authorized JavaScript origins’.
  4. Input http://localhost:8080/ for ‘Authorized redirect URIs’.
  5. Click ‘Save’.
READ  Build a Video Converter Application in Node.js Using Fluent-FFmpeg Library Full Example

 

 

Click ‘Download JSON’ on the right side of Client ID to download client_secret_<really long ID>.json.

 

The downloaded file has all authentication information of your application. Rename the file to “client_secrets.json” and place it in your working directory.

Create app.py file and copy and paste the following code.

 

from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

 

If you run this python script at this time you will see a browser allowing to select your account and grant permission to allow to upload files to google drive. So execute this script and get the access token

READ  Python OpenCV Tutorial To Capture Images From Webcam Full Project

 

Uploading Files in Google Drive

 

Now in this block of code we will be uploading files to google drive with pydrive library. So copy paste this code just below the authentication code.

 

from pydrive.drive import GoogleDrive

drive = GoogleDrive(gauth)

file1 = drive.CreateFile({'title': 'Hello.txt'})  

file1.SetContentString('Hello World!') 

file1.Upload()

 

 

Full Source Code

 

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth() 

drive = GoogleDrive(gauth)

file1 = drive.CreateFile({'title': 'Hello.txt'})

file1.SetContentString('Hello World!') 

file1.Upload()

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :