0
votes

I'm attempting to access a spreadsheet that's under the "google.com" domain using the Google Drive Java API through a service account. I need to export several of the worksheets contained within the document.

I've been able to create and use a service account (used via the Java API) but am not sure how to go about access, or if what I'm trying to do is even viable. I saw the following page: https://developers.google.com/drive/web/delegation but it seems highly unlikely that a service account I'm working with will be granted access to the google.com corporate domain. Our account contact at Google suggested we check the forums for some advice.

Given this, I have a couple of questions about approach:

  • Am I misunderstanding the access requirements? More specifically is there a way to grant a service account access to a specific document?
  • Is this task (worksheet export) better suited to the Spreadsheets API? That API seemed focused on operating within a spreadsheet, and the drive API seems to deal with file/document management.
  • Will a copy of the document to a different domain be required?

My guess is that we will need to copy the document, but I wanted to be sure, and to make sure that I'm understanding the APIs and access correctly.

Thanks!

2
some clarification so I can get a sense of what you're looking to achieve... why are you using a service account? What format are you exporting to? When you say "several of the worksheets contained within", that kinda leads to the spreadsheet API, which as you say is the API that operates within the document.pinoyyid
I'm using a service account because we have a software service running that needs to retrieve the spreadsheet on a scheduled basis without any manual intervention.Patrick Garrity
You'd be better off using a regular account instead of a service account. See stackoverflow.com/questions/19766912/… for how to do that. As for "is spreadsheet api better", it all depends what you want to do. Are you happy downloading the entire file (Drive API), or would you rather be able to access regions of the file (Spreadsheet API)?pinoyyid
Thanks for the reference and clarification on spreadsheet/drive. Based on what you said, my use case fits the Drive API since we have no need to grab regions of the file. I think I'm sticking with a service account -- we don't have a permanent user account to use for regular access, and we don't want to deal with possible token expiration. The regular account method works, but based on the linked thread it feels more like a workaround for using a service account.Patrick Garrity

2 Answers

8
votes

You also have to grant the service account in the drive file you want to access to. Just Share the document you want to get access with the EMAIL ADDRESS that appears in the Google Developers Console (e.g. [email protected])

2
votes

Do you have access to the file? try to use the method files.get https://developers.google.com/drive/v2/reference/files/get if you receive a 200 response then you have access, try to find the parameter exportlinks with those links you can export the file to different formats. otherwise you won't be able to do it.

  • Only people that have been granted access to the file, can access it. in the case of service account with delegation approved by the domain admin, the service account can impersonate people in that domain. if the user it is impersonating does not have access to a certain file, the service account wont have access either.

  • To export the file is better with the Drive API, as you mentioned the Spreadsheets API is to manage the content of the spreadsheet.

  • If you want to modify the information without modifying the original file, then yes.

hope this helps.