ngdrive
A simple ngdrive utitily to download files from gdrive folders recursively into local machine.
Google allows/supports accessing google drive files via API. Below steps are pre-requisites to do the same.
- First a gmail account is needed.
- An app must be created via console.developers.google.com eg: app-name: 'gdrive-syncher'. This can be 'Desktop App'.
- credentials.json file from above app must be placed in the config/ folder.
- 'Google Drive API' must be enabled for the above app via 'APIs & Services' -> 'Enable API & Services'.
Above app must be registered with:
OAuth consent screen
- App Info:
- app name: gdrive-syncher
- support email: email given in point 1 above
- app logo optional
- App Domain:
- application home page: https://gmail.com
- Authorized domains:
- gmail.com
- Developer contact information
- email given in point 1 above
- App Info:
- Scope
- Google Drive API
- Test users
- email given in point 1 must be added as test user
Installation
Add this line to your application's Gemfile:
gem 'googledrive'
And then execute:
$ bundle
Or install it yourself as:
$ gem install googledrive
Usage
require 'googledrive'
gd = Ngdrive::GdriveUtil.new
gd.download_files
Above lines will pickup the files/folders present in the gdrive.folder_id value in settings.yml and download all files recursively into ./downloads folder along with their respective folder names.
Notes:
- google drive maintains a session (via token) valid for a certain amount of time. This session is based on a token and will expire after the duration.
- each time a request is made to google drive via API, it needs this token to allow google drive to continue accessing.
First time when 'Ngdrive::GdriveUtil.new' is run, the gem does below steps:
- it prompts user with a url to copy and enter in the browser. This is OAuthentication request asking the user to allow the access to drive via gmail account. This consent is mandatory as per google rules and regulations.
- When prompted with url on console, user has to enter the url in the browser, allow access, continue.
- once confirmed, google returns a url with format similar to below:
- 'https://....code=<
>&aa=...' code received between << and >> is what is needed as initial token. - users have to copy this and enter in the console to allow gem to create config/token.yaml to keep the token for subsequent calls.
- This token will expire after certain time, in which case, users have to do the url copy paste each time they need to access drive via API.
once token.yaml file is created, accessing and downloading googledrive files is as simple as running: gd.download_files