Python Upload Files to Google Drive Using PyDrive Library Full Tutorial

Python Upload Files to Google Drive Using PyDrive Library Full Tutorial

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

x
Python Upload Files to Google Drive Using PyDrive Library Full Project For Beginners video
 

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

 

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.

READ  Join 10+ Ionic Software Developers Whatsapp Group Invite Links For Ionic Software Developers and Programmers in 2021-22

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’.

 

 

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()

 

READ  Join Competitive Programming Whatsapp Group Invite Links For HTML CSS and Javascript Software Developers and Programmers in 2021-22

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

 

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 Reply