3

What will extra setting to get files / folder from google drive API with service account? Currently getting empty results.

Same code working with credential web scope with client id and secrets key but not with service account.

Vipin Garg
  • 466
  • 3
  • 15
  • Do you want to list the files in your account (other user account) with the services account? – Giselle Valladares Jul 04 '22 at 16:15
  • We will use just single account with our company domain. All files and folders will be share to that account. – Vipin Garg Jul 04 '22 at 18:55
  • Have you tried impersonating the user with the services account using ["delegate domain-wide authority"](https://developers.google.com/identity/protocols/oauth2/service-account#authorizingrequests)? That might help, you can impersonate the user that will have access to all the files and folders. – Giselle Valladares Jul 04 '22 at 18:59
  • Please edit your question and include your code [example] or at very least tell me what programming language you are using so i can show you some code. – Linda Lawton - DaImTo Jul 04 '22 at 19:26
  • @GiselleValladares google drive doesn't require delegation. You can do it without it. – Linda Lawton - DaImTo Jul 04 '22 at 19:27
  • @DalmTo, Yes, it is not necessary if they share the files and folders with the service accounts, or when the files are owned by the services account. However, if they want to skip the step to share the file with the service account, then they can use delegation. I recommended using delegation since the OP mentioned they are handling files from the domain, and it should be feasible for them because they seem to be using Google Workspace. – Giselle Valladares Jul 04 '22 at 19:45
  • The question does not mention workspace if they are using workspace the question needs to be edited to state that – Linda Lawton - DaImTo Jul 05 '22 at 06:59
  • I am using PHP google client api. https://github.com/googleapis/google-api-php-client – Vipin Garg Jul 05 '22 at 19:01
  • Are you using a Google Workspace account? – Giselle Valladares Jul 05 '22 at 19:13
  • If you are then, you can use: `$client->setSubject($user_to_impersonate);` to impersonate the user in your domain that has all the files shared with it. You can read more information [here](https://github.com/googleapis/google-api-php-client#authentication-with-service-accounts) – Giselle Valladares Jul 05 '22 at 21:53

1 Answers1

5

When you use a web client. It pops up and requests authorizaiton. In this way a user is granting your application access to their data. So a file.list will return the files on the users google drive account.

The same thing is happening with your service account. Only service accounts are pre authorized. Your file.list is returning no files becouse the service account doesnt have acess to any files yet. You see it has its own drive account.

Options.

  1. Upload files to the service accounts drive account.
  2. open your credetials.json file. Look for the service account email address. Take that address and share a directory or file on your personal drive account with the service account.

Now do a file.list. It should have files.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449