Skip to content

Instantly share code, notes, and snippets.

@barberj
Last active December 17, 2015 22:09
Show Gist options
  • Save barberj/5680202 to your computer and use it in GitHub Desktop.
Save barberj/5680202 to your computer and use it in GitHub Desktop.
Attempting to get spreadsheets from google drive using google api client
require 'google/api_client'
require 'launchy'
load 'auth.rb'
def client
@client
end
def drive
@drive
end
def session
unless client
@client = Google::APIClient.new
@drive = client.discovered_api('drive', 'v2')
client.authorization.client_id = client_id
client.authorization.client_secret = secret
client.authorization.scope = 'https://www.googleapis.com/auth/drive'
client.authorization.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
uri = client.authorization.authorization_uri
Launchy.open(uri)
$stdout.write "Enter authorization code: "
client.authorization.code = gets.chomp
client.authorization.fetch_access_token!
end
client
end
#application/vnd.google-apps.spreadsheet
def list_files
session
files = []
# no matter how i go about trying to filter i always get all my docs...
params = "'mimeType' = 'application/vnd.google-apps.spreadsheet' and 'trashed' = false"
params = {'query' => {'mimeType' => 'application/vnd.google-apps.spreadsheet', 'trashed' => false}}
params = {'q' => {'mimeType' => 'application/vnd.google-apps.spreadsheet', 'trashed' => false}}
params = {'q' => "'mimeType' = 'application/vnd.google-apps.spreadsheet' and 'trashed' = false"}
params = {'mimeType' => 'application/vnd.google-apps.spreadsheet', 'trashed' => false}
begin
rsp = client.execute :api_method => drive.files.list, :parameters => params
#rsp = client.execute :api_method => drive.files.list, :q => params # have tried as well
files += rsp.data.items
puts "Token was #{params['pageToken']} but now #{rsp.next_page_token}"
params['pageToken'] = rsp.next_page_token unless rsp.next_page_token.nil?
end while !rsp.next_page_token.nil?
files
end
@sqrrrl
Copy link

sqrrrl commented May 30, 2013

The correct way to do the query is

params = {'q' => "mimeType = 'application/vnd.google-apps.spreadsheet' and trashed = false"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment