Created
July 2, 2015 16:53
-
-
Save aurelian/f45bdfc9c1f7a4b6f17d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# gem 'google-api-client', '~> 0.7.1', require: 'google/api_client' | |
require 'rubygems' | |
require 'bundler/setup' | |
require 'google/api_client' | |
require 'google/api_client/client_secrets' | |
require 'google/api_client/auth/installed_app' | |
require 'google/api_client/auth/file_storage' | |
SCOPES = [ | |
'https://www.googleapis.com/auth/drive', | |
'https://www.googleapis.com/auth/drive.file', | |
'https://www.googleapis.com/auth/drive.readonly', | |
'https://www.googleapis.com/auth/drive.metadata.readonly', | |
'https://www.googleapis.com/auth/drive.appdata', | |
'https://www.googleapis.com/auth/userinfo.email', | |
'https://www.googleapis.com/auth/userinfo.profile' | |
] | |
client = Google::APIClient.new( | |
:application_name => 'osea', | |
:application_version => '0.0.1' | |
) | |
client_secrets = Google::APIClient::ClientSecrets.load | |
credential_storage = Google::APIClient::FileStorage.new "secrets.data" | |
unless credential_storage.authorization | |
flow = Google::APIClient::InstalledAppFlow.new( | |
:client_id => client_secrets.client_id, | |
:client_secret => client_secrets.client_secret, | |
:scope => SCOPES | |
) | |
client.authorization = flow.authorize(credential_storage) | |
else | |
client.authorization = credential_storage.authorization | |
end | |
# help. | |
def print_files_in_folder(client, folder_id) | |
drive = client.discovered_api('drive', 'v2') | |
page_token = nil | |
begin | |
parameters = {'folderId' => folder_id} | |
if page_token.to_s != '' | |
parameters['pageToken'] = page_token | |
end | |
result = client.execute( | |
:api_method => drive.children.list, | |
:parameters => parameters) | |
puts result.status | |
if result.status == 200 | |
children = result.data | |
children.items.each do |child| | |
puts "File Id: #{child.id}" | |
end | |
page_token = children.next_page_token | |
else | |
puts "An error occurred: #{result.data['error']['message']}" | |
page_token = nil | |
end | |
end while page_token.to_s != '' | |
end | |
# /help. | |
# print_files_in_folder client, "root" | |
# exit | |
drive = client.discovered_api('drive', 'v2') | |
parameters = {'folderId' => 'root'} | |
result = client.execute(:api_method => drive.children.list, :parameters => parameters) | |
# puts result.data.inspect | |
children = result.data | |
children.items.each do |child| | |
puts "File Id: #{child.id}" | |
result = client.execute(:api_method => drive.files.get, :parameters => {'fileId' => child.id}) | |
if result.status == 200 | |
file = result.data | |
puts "\tTitle: #{file.title}" | |
else | |
puts "An error occured: #{result.data['error']['message']}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment