0

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!

Patrick Garrity
  • 240
  • 2
  • 3
  • 10
  • 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 Jan 31 '15 at 04:37
  • 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 Feb 02 '15 at 15:48
  • You'd be better off using a regular account instead of a service account. See http://stackoverflow.com/questions/19766912/how-do-i-authorise-a-background-web-app-without-user-intervention-canonical?s=6|2.1978 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 Feb 02 '15 at 16:09
  • 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 Feb 02 '15 at 16:31

2 Answers2

8

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. XXXXX@developer.gserviceaccount.com)

jalogar
  • 1,426
  • 16
  • 24
  • Thanks! Upvoting because it's useful info for anyone else who sees this Q. I figured that out after accepting a solution and doing some more experimenting, I (perhaps stupidly) didn't realize you could share directly with the service acct. It makes life easier. – Patrick Garrity Mar 18 '15 at 16:33
  • @jalogar I just did but I cannot see the shared folder from my service account – pantonis May 15 '19 at 11:42
2

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.

Gerardo
  • 2,422
  • 1
  • 10
  • 12
  • Thanks! I do not have access to the file. Cannot retrieve by ID, and attempting to get the file directly results in a 403 Forbidden. If admin-approved delegation is going to be required, it looks like I'll need to migrate the file to a different domain. – Patrick Garrity Feb 02 '15 at 15:55
  • Accepting this answer because it helped me confirm that I do not have access, confirmed how access for service accounts works, and I now have a path forward working with a copy of the document under a different domain. – Patrick Garrity Feb 02 '15 at 16:27