Skip to content

Instantly share code, notes, and snippets.

@ewalk153
Created December 14, 2022 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewalk153/00fbbb8e0e0eb745284b86067172d4c8 to your computer and use it in GitHub Desktop.
Save ewalk153/00fbbb8e0e0eb745284b86067172d4c8 to your computer and use it in GitHub Desktop.
# 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