Last active
October 20, 2015 00:09
-
-
Save davidbody/784603f73a7d091243e3 to your computer and use it in GitHub Desktop.
Read a Google spreadsheet from Ruby
This file contains 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
google-apis |
This file contains 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
2.2.3 |
This file contains 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
source "https://rubygems.org" | |
gem "google_drive", "~> 1.0.1" |
This file contains 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 | |
remote: https://rubygems.org/ | |
specs: | |
activesupport (4.2.4) | |
i18n (~> 0.7) | |
json (~> 1.7, >= 1.7.7) | |
minitest (~> 5.1) | |
thread_safe (~> 0.3, >= 0.3.4) | |
tzinfo (~> 1.1) | |
addressable (2.3.8) | |
autoparse (0.3.3) | |
addressable (>= 2.3.1) | |
extlib (>= 0.9.15) | |
multi_json (>= 1.0.0) | |
extlib (0.9.16) | |
faraday (0.9.2) | |
multipart-post (>= 1.2, < 3) | |
google-api-client (0.8.6) | |
activesupport (>= 3.2) | |
addressable (~> 2.3) | |
autoparse (~> 0.3) | |
extlib (~> 0.9) | |
faraday (~> 0.9) | |
googleauth (~> 0.3) | |
launchy (~> 2.4) | |
multi_json (~> 1.10) | |
retriable (~> 1.4) | |
signet (~> 0.6) | |
google_drive (1.0.1) | |
google-api-client (>= 0.7.0) | |
nokogiri (>= 1.4.4, != 1.5.2, != 1.5.1) | |
oauth (>= 0.3.6) | |
oauth2 (>= 0.5.0) | |
googleauth (0.4.2) | |
faraday (~> 0.9) | |
jwt (~> 1.4) | |
logging (~> 2.0) | |
memoist (~> 0.12) | |
multi_json (~> 1.11) | |
signet (~> 0.6) | |
i18n (0.7.0) | |
json (1.8.3) | |
jwt (1.5.1) | |
launchy (2.4.3) | |
addressable (~> 2.3) | |
little-plugger (1.1.4) | |
logging (2.0.0) | |
little-plugger (~> 1.1) | |
multi_json (~> 1.10) | |
memoist (0.12.0) | |
mini_portile (0.6.2) | |
minitest (5.8.1) | |
multi_json (1.11.2) | |
multi_xml (0.5.5) | |
multipart-post (2.0.0) | |
nokogiri (1.6.6.2) | |
mini_portile (~> 0.6.0) | |
oauth (0.4.7) | |
oauth2 (1.0.0) | |
faraday (>= 0.8, < 0.10) | |
jwt (~> 1.0) | |
multi_json (~> 1.3) | |
multi_xml (~> 0.5) | |
rack (~> 1.2) | |
rack (1.6.4) | |
retriable (1.4.1) | |
signet (0.6.1) | |
addressable (~> 2.3) | |
extlib (~> 0.9) | |
faraday (~> 0.9) | |
jwt (~> 1.5) | |
multi_json (~> 1.10) | |
thread_safe (0.3.5) | |
tzinfo (1.2.2) | |
thread_safe (~> 0.1) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
google_drive (~> 1.0.1) | |
BUNDLED WITH | |
1.10.6 |
This file contains 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
require "googleauth" | |
require "google_drive" | |
# | |
# Set up a service account at https://console.developers.google.com/project | |
# and download the credentials to a JSON file. This includes the service | |
# account's private key. | |
# | |
# Be sure to also enable the Drive API for your Google project. | |
# | |
# Share the Google spreadsheet with the service account email address. | |
# | |
# Set up the following environment variable: | |
# | |
# GOOGLE_APPLICATION_CREDENTIALS="/Users/david/play/drive-api-play/My Project-c6549ad64235.json" | |
# | |
# adjusted for your configuration. The "My Project....json" file is the service | |
# account credentials. | |
# | |
# If you get an error similar to the following | |
# | |
# SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (Faraday::SSLError) | |
# | |
# this means the TLS/SSL certificate chain for the Google APIs could not be | |
# verified. This is most likely because your Ruby can't find the correct root | |
# certificates. One way to solve this is set the following environment variable: | |
# | |
# SSL_CERT_FILE=/Users/david/.rvm/gems/ruby-2.2.1@google-api/gems/google-api-client-0.8.6/lib/cacerts.pem | |
# | |
# You can do this in Ruby code with | |
# | |
# ENV["SSL_CERT_FILE"] = $LOAD_PATH.grep(/google-api-client/).first + "/cacerts.pem" | |
# | |
# If you are using RVM on Mac OS X, you can also solve the problem by re- | |
# installing Ruby as follows: | |
# | |
# % rvm reinstall VERSION --disable-binary | |
# | |
spreadsheet_name = "demo" | |
credentials = Google::Auth.get_application_default | |
credentials.scope = ["https://www.googleapis.com/auth/drive", "https://spreadsheets.google.com/feeds/"] | |
credentials.fetch_access_token! | |
access_token = credentials.access_token | |
drive_session = GoogleDrive.login_with_oauth(access_token) | |
spreadsheet = drive_session.spreadsheet_by_title(spreadsheet_name) | |
raise "Spreadsheet #{spreadsheet_name} not found" unless spreadsheet | |
worksheet = spreadsheet.worksheets.first | |
worksheet.rows.each do |row| | |
puts row.join(', ') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment