Skip to content

Get workspace drive files along with sharing information #3603

@sagar-rodda

Description

@sagar-rodda

I have a requirement to get the list of files from drive which are shared in last 7 days within my company google workspace.

For example:
[file name: abc.txt
shared on: 19th Dec
shared with: Rick
shared by: Shane
role given: viewer]

I tried below steps.

  1. Create a service account.
  2. Add the service account in domain-wide-delegated list.
  3. Use the service account key to make files.list API.

Initially it was returning empty list. I shared few files with the service account then I am able to see those shared files but that's not my requirement.

Is it possible to achieve the requirement or do I need to use a different API?

provided the script below for reference.

const { google } = require('googleapis');
const path = require('path');

// Path to your service account key file
const SERVICE_ACCOUNT_FILE = path.join(__dirname, 'doman_wide_delegation_service_account.json');

// Scopes for accessing Google Drive
const SCOPES = ['https://www.googleapis.com/auth/drive'];

async function listDriveFiles() {
	try {
		// Authenticate using the service account
		const auth = new google.auth.GoogleAuth({
			keyFile: SERVICE_ACCOUNT_FILE,
			scopes: SCOPES,
		});

		const drive = google.drive({ version: 'v3', auth });

		// List files in Google Drive
		const response = await drive.files.list({
			pageSize: 10, // Adjust the number of files you want to retrieve
			fields: 'nextPageToken, files(id, name)',
			// corpora: "domain",
			// q: "trashed = false"
			includeItemsFromAllDrives: true,
			supportsAllDrives: true
		});

		const files = response.data.files;

		if (files.length) {
			console.log('Files:');
			files.forEach((file) => {
				console.log(`${file.name} (${file.id})`);
			});
		} else {
			console.log('No files found.');
		}
	} catch (error) {
		console.error('Error listing files:', error.message);
	}
}


listDriveFiles();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions