Analytics Vidhya
Published in

Analytics Vidhya

Pydrive to download from Google Drive to a remote machine


On your remote machine where you want to download your files from Google drive, install PyDrive with pip command

$ pip3 install PyDrive

2. OAuth AUTHENTICATION : Downloading client_secrets.json file.

Drive API requires OAuth2.0 for authentication. Go to APIs Console and create your project. When you click on the link, you will be taken to your Googe cloud developer console. Click on Create Project to create a new project. Search for ‘Google Drive API’ in your project Dashboard , select the entry, and click ‘Enable’.

Select ‘Credentials’ from the left menu, click ‘Create Credentials’. Click on Configure consent screen.

Configure the OAuth consent as shown below.

After the OAuth consent is done, click on OAuth client ID.

Choose other for Application Type and give a name for the OAuth Client ID and click Create.

Once the OAuth client ID is created, you can download the ‘client_***************.json’. This json file has all authentication information of your application. Rename the file to “client_secrets.json” and place it in your working directory.


Create file in the same folder where you have the client_secrets.json file with the code below. We are using CommanLineAuth() because CommandLineAuth() takes code from user at command line. Type the following commands in

from pydrive.auth import GoogleAuthfrom import GoogleDrivegauth = GoogleAuth()gauth.CommandLineAuth()drive = GoogleDrive(gauth)

When you run with the, you will get the link to get the verification code as shown below. Copy the link to the browser and get the verification code from the browser and paste it.

Once you enter the verification code- You will get ‘Authentication successful’.

To get a list of all the files in your drive add this code to

file_list = drive.ListFile({‘q’: “‘root’ in parents and trashed=false”}).GetList()for file in file_list:
print(‘title: %s, id: %s’ % (file[‘title’], file[‘id’]))

This will list all the title and id of your files in your root directory.

To download all the files in a particular directory get the id of the directory and use GetContentFile() to download all the files.

file_list = drive.ListFile({‘q’: “‘*******-4GVR2j****RdPSGquB-***’ in parents and trashed=false”}).GetList()for file in file_list:
print(‘title: %s, id: %s’ % (file[‘title’],file[‘id’]))

Everytime you run the, you will be asked for the verification code. In order to automate the verification, you can add a settings.yaml file to your folder. Add the following code to settings.yaml file. For ‘client_id’ and ‘client_secret’ under client_config, copy them from your client_secrets.json file. Also, create an empty file called creds.json in the folder using vim.

Note — creds.json will get all the credentials automatically when u run for the first time. So this is just empty to begin with.

client_config_backend: ‘settings’
client_id: “{client_id from client_secrets.json}”
client_secret: “{client_secret from client_secrets.json}”
save_credentials: True
save_credentials_backend: ‘file'
save_credentials_file: ‘creds.json’
- “"

Basically, the current working folder should have all the following files.

After adding the settings.yaml file, when you run for the first time, you will be prompted to enter the verification code. Once you have successfully authenticated the first time, your login credentials get saved to creds.json file. When you run the later, authentication happens automatically.

Code is available at :




Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem

Recommended from Medium

Databricks: Upsert to Azure SQL using PySpark

The Unicorn Project — A Book Review

ESP32 + AWS IoT And Secured WiFi LEDs

Which is the best Programming language to learn first?

Improve Postgresql Performance by Reducing Dead Tuple without Auto-vacuum

Using git and GitHub (forking and pull requests)

Architecture and Workflow Overview

How to Reset AINOL Novo 7 Flame

Hard reset Samsung galaxy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bhuvana Kundumani

Bhuvana Kundumani

Data Scientist (NLP) @ Shell

More from Medium

Machine Learning for Retrofits

A Lot of Cool Techniques Are Throwing to ME in General Assembly Bootcamp!

How do FANG (now MANG)companies use Machine Learning (ML)?

Kickstart to career in AI with Meta’s RAISE Program