I have a Google Workspace account and am a Super Admin on the domain I want to pull together a list of all files owned by all users using Google Apps Script so I can use the triggers to run it every day to update the numbers
I have a script that gets all users and loops through each user to get a list of files where the user is the owner using the Drive API. This works, however it doesn't get all the files a user owns, just the files that the account running the script has access to that are owned by the user. For example user1@domain.com has 712 files, 12 of which are shared with admin@domain.com. When admin@domain.com runs the script the API returns 12 files for user1@domain.com. If user1@domain.com runs the script the API returns all 712 files.
This is the section of script I currently have:
// Get all users in the domain
do {
page = AdminDirectory.Users.list({
domain: myDomain,
orderBy: 'givenName',
maxResults: 100,
pageToken: pageToken
});
const users = page.users;
if (!users) {
console.log('No users found.');
return;
}
// For each user, count the number of file types
for (const user of users) {
// set the query to look for all files onwed by the user and not trashed
const query = '"' + user.primaryEmail + '" in owners and trashed = false';
// Clear the filesAll array, the files holding array, the counter and the pageToken ready for the next user
const filesAll = [];
let files;
let fileCount = 0;
let pageToken2;
do {
try {
files = Drive.Files.list({
q: query,
corpora: 'allDrives',
includeItemsFromAllDrives: true,
supportsAllDrives: true,
maxResults: 100,
pageToken: pageToken2
});
So what I think I need to do is to run the Drive API "as the user" and not as the admin, is this possible? I found the script and service account in Cloud Platform and got the Client ID then added this to Domain Wide Delegation, so that side is done, it is just how I tell the script to access the Drive API as the user and not as the Admin, there doesn't seem to be an option to do that in the API itself from within Apps Script.
I have tried googling for a solution, but the only things I have found are very old and use features no longer available, or else give options for using something other than Apps Script