catch22
catch22

Reputation: 1693

download link for google drive object with service account

I'm building an api that uses google drive with a service account.

Google Drive objects come with a @web_view_link property that looks like:

"https://drive.google.com/file/d/0B9eyEerjltIgZ2dyUTB1eGNjYUk/view?usp=drivesdk"

When I access this @web_view_link in the browser using a personal account, the link is functional and the object can be downloaded from there.

When I paste the @web_view_link in the browser using a service account, Google Drive complains about permissions and I cannot access it, even tho I am accessing with an account owner.

Is it possible to access a download link from google drive using a service account?

I'm using google-drive-ruby gem and this is the code to retrieve that @web_view_link url:

def create_url
  folder_name = Discourse.current_hostname
  found = google_files.select { |f| f.id == id }
  file_title = found.first.title
  file_url = session.collection_by_title(folder_name).file_by_title(file_title).human_url
end

Upvotes: 1

Views: 6478

Answers (1)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17651

service accounts are not your personal accounts. Service accounts belong to your application. Make sure you've enabled Drive API when you generated the service account and set the role to owner or editor.

This snippet from SO post might also help with regard to downloading using service account in ruby:

    require 'googleauth'
    require 'google/apis/drive_v2'

    Drive = Google::Apis::DriveV2

    upload_source = "/User/my_user_name/hacking.txt"
    drive = Drive::DriveService.new
    # Drive::AUTH_DRIVE is equal to https://www.googleapis.com/auth/drive
    drive.authorization = Google::Auth.get_application_default([Drive::AUTH_DRIVE])
    file = drive.insert_file({title: 'hacking.txt'}, upload_source: upload_source)
    drive.get_file(file.id, download_dest: '/tmp/my_file.txt') 

Upvotes: 1

Related Questions