I'm trying to get the content of a Google Drive spreadsheet, but I can't seem to find a gem that can do it easily. I tried google-drive-ruby, but it involves a step in which I have to get an auth token from Google's website. This is not very helpful, as I have to do all this server-side. Apparently there was a login method in previous versions of the gem, but got removed.
Is there any way of doing this? Do I have to use OAuth to get a token and pass that token onto google-drive-ruby?
I managed to get an answer on Reddit. You need a Service Account and to share the document with your service app's email. The p12 key has to be stored somewhere as well.
# gem install google-api-client -v 0.8.6 # upper versions are not compatible with this code
require "google/api_client"
@email = "[email protected]"
@key = "path/to/key.p12"
key = Google::APIClient::KeyUtils.load_from_pkcs12(@key, 'notasecret')
auth = Signet::OAuth2::Client.new(
token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
audience: 'https://accounts.google.com/o/oauth2/token',
scope: ["https://www.googleapis.com/auth/drive", "https://spreadsheets.google.com/feeds/"].join(' '),
issuer: @email,
access_type: 'offline',
signing_key: key
)
auth.fetch_access_token!
puts auth.access_token
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With