Download Folders and Files using Google Drive API and Python

Before everything, let’s create an OAuth 2.0 token for authentication at the Google API Console.

If you already have one, you can jump to SECTION 2: DOWNLOAD GOOGLE DRIVE FILES AND FOLDERS.

SECTION 1: AUTHENTICATION

1. Create a New Project

Go to Google API Console using your Google account. Create a new project.

Provide a project name.

2. Enable Google Drive API in API library

Go to ‘Library’ tab.

Search ‘Google Drive API’.

Enable it.

3. Create an OAuth Authentication

Go to ‘OAuth consent screen’ tab, choose if you want it for internal use or external use, and create.

Provide some information. (Only App information — App name, User support email, and Developer contact information — Email Addresses must be filled.)

Add emails as Test users if you are using external user type.

Save and Continue.

4. Create an OAuth Client and Download Generated JSON File

Go to ‘Credentials’ tab and click ‘CREATE CREDENTIALS’, at last, ‘OAuth client ID’.

Choose one application type.

In the pop-up, choose ‘DOWNLOAD JSON’.

You can now proceed to the download code. 🎉

SECTION 2: DOWNLOAD GOOGLE DRIVE FILES AND FOLDERS

Here are two samples (link_1, link_2) I made to play with, feel free to download them and put them on your Google Drive to try out. But remember to change the IDs we use in the examples below to IDs in your Google Drive.

  • Sample 1
google-api-sample
|
|- folder_1/
| |- sample.png
|- folder_2/
| |- folder_2A
| | |- sample.txt
|- sample.jfif
  • Sample 2
google-api-sample-2
|
|- Scaling to Very Very Large Corpora for Natural Language Disambiguation.pdf

1. Git Clone

The code is provided on GitHub (link). Git clone it to follow the rest!

git clone https://github.com/Techyhans/google-api-scraping.git

2. Add client_secret.json

Place your JSON file created in SECTION 1 in the google-api-scraping folder and rename it to client_secret.json.

3. Get the Folder or Files ID.

  • Folder ID:

You can get the folder ID from its URL, which is shown in the red rectangle below. It is the ID of Sample 1.

  • File ID:

You can get the file ID after opening them in a new window.

4. Run the command to download files or folders.

You may state two arguments to run with the download.py.

  • -i’ or ‘--id’: One or more files or folders ID you would like to download (Must be given)
  • -n’ or ‘ -- name’: One or more folder names you would like to search and download in given parent folder ID (Optional).”

CASE 1: Download folders or files with their ID

  • If you want to download a folder with all items it contains. For example, we download Sample 1:
python download.py -i 1ZyjCpwSb9EtkWYnWB6k_PerulHhBxkRA
  • If you want to download multiple folders. For example, we download both Sample 1 and Sample 2:
python download.py -i 1ZyjCpwSb9EtkWYnWB6k_PerulHhBxkRA 1ZbWtRi1HqqzPzD8ldyE52LympemQ15Ze
  • If you want to download a file, also, you only have to provide the ID. For example, we download the sample.jfif in Sample 1:
python download.py -i 16Cyd8JMnO-U_t0hj2rFsitFRz07smIpk

CASE 2: Download specifically named folders with the given parent folder ID

  • For example, to search a folder with the folder name folder_1 under the parent folder, Sample 1, with its ID:
python download.py -i 1ZyjCpwSb9EtkWYnWB6k_PerulHhBxkRA -n folder_1

Note:

When you run the download.pyfor the first time, you will have to verify your Gmail account to connect to the Google Drive API.

Choose ‘Continue’ to confirm access.

You will receive this message when you connect successfully. Then the download.py will continue to download the folders or files and you don’t have to reconnect the next time, token_drive_v3.pickle saves the connection.

Github

You can find the code used at this site.

Reference

  1. https://developers.google.com/drive/api/v3/enable-drive-api
  2. https://developers.google.com/drive/api/v3/manage-downloads
  3. https://developers.google.com/drive/api/v3/search-files
  4. https://developers.google.com/identity/protocols/oauth2#1.-obtain-oauth-2.0-credentials-from-the-dynamic_data.setvar.console_name-.

About Author

This article is written by Han Sheng, Junior Artificial Intelligence Engineer in CertifAI, Penang, Malaysia. He has a passion for Deep Learning, Computer Vision and also Edge Devices. He made several AI-based Web/Mobile Applications to help clients solving real-world problems. Feel free to read about him via his portfolio or Github profile.

--

--

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
Draden Liang Han Sheng

Draden Liang Han Sheng

Full Stack AI Application Development | Computer Vision | Deep Learning | Edge Devices