Prashant Khaire
Prashant Khaire

Reputation: 137

Google Drive API - How to work with the domain-wide service account and get all document list from domain

I was trying to use the service account of my App to access to all documents of all users in my domain. I was following these instructions: https://developers.google.com/drive/delegation

But it didn't work. Could you please help me out on this??

Here is the problematic line of code

GoogleCredential credential = new GoogleCredential.Builder() .setTransport(TRANSPORT) .setJsonFactory(jsonFactory) .setServiceAccountId("SERVICE_ACCOUNT_EMAIL") .setServiceAccountPrivateKeyFromP12File( new java.io.File("SERVICE_ACCOUNT_PKCS12_FILE_PATH")) .setServiceAccountScopes(scopes) .setServiceAccountUser("abc@xyz.com").build();

It's working fine, but it should give me details for all the user for this domain... Instead it gives me only for one useraccount.. In the "Service" account doc it says it should be able to access all the user's data under this domain.

Scopes are added and APIS are enabled for this account

Upvotes: 2

Views: 1803

Answers (1)

omerio
omerio

Reputation: 1196

I don't think the docs meant that you can have a service account and in one go access all the files in the whole domain, you need to act on behalf of a particular user by using the service account .setServiceAccountUser("abc@xyz.com").

The service account simply saves you the hassle of having to request permission from individual users. The only way I think you can achieve what you want is to retrieve all the users in the Domain using the Directory API in the Admin SDK (see this answer Listing users using Google Admin SDK in Java) then loop through these users creating a new Credential object by setting the service account user e.g. setServiceAccountUser("blah@blah.com").

Then use the Drive SDK to list the files for each user:

https://developers.google.com/drive/v2/reference/files/list

Upvotes: 3

Related Questions