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

旧城冷巷雨未停 提交于 2021-01-27 21:18:51

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/26236083/google-drive-api-how-to-work-with-the-domain-wide-service-account-and-get-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!