Pezholio
Pezholio

Reputation: 2694

Google Drive Ruby - accessing files via a service account

I've been using the Google Drive Ruby gem to access information from Google spreadsheets, and, with the impending deprecation of a lot of stuff, have been forced to upgrade, and can now no longer log on with a simple username and password (I understand this is being deprecated too)

After flipping various options and making certain incantations, I've managed to get logged in via Oauth like so:

client = Google::APIClient.new(application_name: 'Google Drive Ruby test', application_version: '0.0.1')
key = Google::APIClient::KeyUtils.load_from_pkcs12(
  'config/key.p12',
  ENV['GAPPS_KEY_PASSWORD']
)

asserter = Google::APIClient::JWTAsserter.new(
  ENV['GAPPS_SERVICE_ACCOUNT_EMAIL'],
  ['https://www.googleapis.com/auth/drive'],
  key
)

client.authorization = asserter.authorize
auth_token = client.authorization.access_token

google_drive = GoogleDrive.login_with_oauth(access_token)

This works fine, however, I only seem to be able to act as my service account, so don't have access to the files that other users have access to. For example:

google_drive.spreadsheet_by_key("18rNqAz3R0hhp7OYEPLU8thGHsUR0jgYgHk3zQ8CWL-4")

Returns:

Google::APIClient::ClientError: File not found: 18rNqAz3R0hhp7OYEPLU8thGHsUR0jgYgHk3zQ8CWL-4

I understand that you can act as a given user, but despite trawling through the docs, I don't seem to be able to get this to work. Any ideas?

Upvotes: 1

Views: 867

Answers (1)

Sandip Karanjekar
Sandip Karanjekar

Reputation: 850

You have to share your spreadsheet with this client mail id - ENV['GAPPS_SERVICE_ACCOUNT_EMAIL']

Otherwise you won't be able to access anything even though you have a valid token.

Upvotes: 1

Related Questions