Search code examples
javagoogle-apigoogle-drive-apiservice-accounts

Google Drive API - How to work with the domain-wide service account


I was trying to use the service account of my app to access to all documents of all users in my domain following this instruction:

https://developers.google.com/drive/delegation But it didn't work. Could you please help me out on this?? I tried to follow URL -

http://iambusychangingtheworld.blogspot.co.uk/2013/12/google-drive-api-how-work-with-domain.html

and 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("[email protected]").build();

its working fine But 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 users data under this domain.. am I missing something>? FYI... Scopes are added , APIS are enabled for this account

Is there any way I can get it?? I have also seen below links but was not of - Not able to access my google drive Files with service account


Solution

  • The service account with domain-wide delegation allows you to access each account's data one at a time.

    If you want to access all the users' data in a domain, you will need to generate a different token for each user (passing the email in the setServiceAccountUser() method) and make the API call for each user.