Created
December 14, 2022 14:29
-
-
Save ewalk153/00fbbb8e0e0eb745284b86067172d4c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Google docs query command-line sample. | |
require 'google/apis/drive_v2' | |
require 'google/api_client/client_secrets' | |
require 'google/api_client/auth/installed_app' | |
require 'google/api_client/auth/storage' | |
require 'google/api_client/auth/storages/file_store' | |
require 'logger' | |
require 'json' | |
CREDENTIAL_STORE_FILE = "#{$0}-oauth2.json" | |
# Handles authentication and loading of the API. | |
def setup | |
storage = Google::APIClient::Storage.new( | |
Google::APIClient::FileStore.new(CREDENTIAL_STORE_FILE)) | |
storage.authorize | |
if storage.authorization.nil? | |
client_secrets = Google::APIClient::ClientSecrets.load | |
# The InstalledAppFlow is a helper class to handle the OAuth 2.0 installed | |
# application flow, which ties in with Stroage to store credentials | |
# between runs. | |
flow = Google::APIClient::InstalledAppFlow.new( | |
:client_id => client_secrets.client_id, | |
:client_secret => client_secrets.client_secret, | |
:scope => ['https://www.googleapis.com/auth/drive.metadata.readonly'] | |
) | |
flow.authorize(storage) | |
end | |
return storage.authorization | |
end | |
def dump_files(auth_client) | |
drive = Google::Apis::DriveV2::DriveService.new | |
files = drive.list_files(options: { authorization: auth_client }) | |
puts JSON.pretty_generate(files.to_h) | |
end | |
if __FILE__ == $0 | |
authorization = setup() | |
dump_files(authorization) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment