Access your Google Drive spreadsheet in Ruby on Rails

I’m trying to get the contents of a Google Drive spreadsheet, but I can’t find a gem that can do this easily. I tried google-drive-ruby , but that includes the step where I should get the authentication token from the Google site. This is not very useful as I have to do all this on the server side. Apparently, in previous versions of the gem there was a login method, but it was deleted.

Is there any way to do this? Should I use OAuth to get a token and transfer this token to google-drive-ruby?

+6
source share
1 answer

I managed to get a response on Reddit. You need a service account and share the document with the email of your service application. The p12 key must also be saved.

# gem install google-api-client -v 0.8.6 # upper versions are not compatible with this code require "google/api_client" @email = " email@developer.gserviceaccount.com " @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 
+12
source

Source: https://habr.com/ru/post/987515/


All Articles