Skip to content

Instantly share code, notes, and snippets.

@raymyers
Created December 18, 2015 19:55
Show Gist options
  • Save raymyers/4b425eb1d1874ab51c64 to your computer and use it in GitHub Desktop.
Save raymyers/4b425eb1d1874ab51c64 to your computer and use it in GitHub Desktop.
Example Google Drive ruby API usage with google-api-client version 0.9.pre4 authenticating with a service account
ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "#{ENV['HOME']}/google-service-account.json"
scopes = ['https://www.googleapis.com/auth/drive']
drive = Google::Apis::DriveV2::DriveService.new
auth_client = Google::Auth.get_application_default(scopes).dup
auth_client.sub = '[email protected]'
drive.authorization = auth_client
report_folders = drive.list_files(q: "title = 'Reports'")
raise "Expected 1 folder called 'Reports', found #{report_folders.items.count}" if report_folders.items.count != 1
parent_id = report_folders.items[0]
source = StringIO.new("Content")
file_metadata = {title: 'test.txt', parents: [parent_id]}
inserted_metadata = drive.insert_file(file_metadata, upload_source: source, content_type: 'text/plain')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment