Analytics Vidhya
Published in

Analytics Vidhya

Pydrive to download from Google Drive to a remote machine

  1. INSTALLING PYDRIVE

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.

3. USING CommanLineAuth() FOR REMOTE MACHINE

Create quickstart.py 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 quickstart.py

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

When you run with the quickstart.py, 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 quickstart.py

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’]))
file.GetContentFile(file[‘title’])

Everytime you run the quickstart.py, 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 quickstart.py for the first time. So this is just empty to begin with.

client_config_backend: ‘settings’
client_config:
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’
oauth_scope:
- “https://www.googleapis.com/auth/drive"

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

quickstart.py
creds.json
settings.yaml
client_secrets.json

After adding the settings.yaml file, when you run quickstart.py 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 quickstart.py later, authentication happens automatically.

Code is available at :https://github.com/bhuvanakundumani/pydrive_4_remotemachine.git

--

--

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