Skip to content

Instantly share code, notes, and snippets.

@Pcushing
Created June 21, 2012 16:26
Show Gist options
  • Save Pcushing/2966841 to your computer and use it in GitHub Desktop.
Save Pcushing/2966841 to your computer and use it in GitHub Desktop.
Log into google docs and parse the data, work in progress
require 'rubygems'
require 'google_drive'
# Go get your consumer key, client_secret, and client_id for Google Drive here https://code.google.com/apis/console/
consumer_key = 'INSERT YOUR CONSUMER_KEY HERE'
client_secret = 'INSERT YOUR CLIENT_SECRET HERE'
client_id = 'INSERT YOUR CLIENT_ID HERE'
client = OAuth2::Client.new(
client_id, client_secret,
:site => "https://accounts.google.com",
:token_url => "/o/oauth2/token",
:authorize_url => "/o/oauth2/auth")
puts "Oh, hey there client #{ client }"
auth_url = client.auth_code.authorize_url(
:redirect_uri => "urn:ietf:wg:oauth:2.0:oob", #Comes from instructions on http://rubydoc.info/github/gimite/google-drive-ruby/master/GoogleDrive.login_with_oauth
:scope =>
"https://docs.google.com/feeds/ " +
"https://docs.googleusercontent.com/ " +
"https://spreadsheets.google.com/feeds/")
puts "We've got ourselves an auth_url #{ auth_url } so why don't you go get
us a PIN:"
authorization_code = gets.chomp
# Redirect the user to auth_url and get authorization code from redirect URL.
auth_token = client.auth_code.get_token(
authorization_code,
:redirect_uri => "urn:ietf:wg:oauth:2.0:oob")
puts "We've got ourselves an auth_token #{ auth_token }. No big deal..."
session = GoogleDrive.login_with_oauth(auth_token)
puts "We've got ourselves a session via oauth #{ session }, yo!"
# First worksheet of
# https://docs.google.com/spreadsheet/ccc?key=0AtsLecjMWFCbdEJwNEpRcW5TMU53QV9GX1pPMllfYUE
ws = session.spreadsheet_by_key("0AtsLecjMWFCbdEJwNEpRcW5TMU53QV9GX1pPMllfYUE").worksheets[0]
puts "and here's a worksheet #{ ws }"
# Dumps of our data into cells in memory, but we should probably print this to a file so we can clean it up into a different file.
for row in 1..ws.num_rows
for col in 1..ws.num_cols
p ws[row, col]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment