Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set api request to google drive api using axios

I working on a google drive project but I am not able to send search parameters with Axios to google API from frontend react

can any explain to me how to write queries like in google drive api url

  1. Search all files and folders by date
  2. Find a file by size
  3. Find all empty folders
  4. Find a file by type such as ppt, image, etc

Axios request look like that

 axios.get('https://www.googleapis.com/drive/v3/files?access_token='+accessToken+'&q=mimeType%3D%22application%2Fvnd.google-apps.folder%22').then(res=>{
    console.log(res)
    setFolders(res.data.files)
}).then(err=>{
    console.log(err)
})

Thanks in advance :)

like image 879
Air University Avatar asked Oct 24 '25 01:10

Air University


2 Answers

This is how you can use the Google Drive API with axios for each of your questions:

  1. Search all files and folders by date

Yes you can! you can use createdTime and modifiedTime filters (see more available query terms here)

  1. Find a file by size

Yes, you can, but, you will need to return the size of the file/folder and filter the resulting files by specific size, you don't need to download the file, but it could be a little inefficient since you need to fetch the files first (use query filters to limit the results).

  1. Find all empty folders

Yes, you can! but similar to the previous point, you need to iterate each folder and search files using the folder id, checking if files are empty. Ensure you're filtering folders only by using mimeType filter application/vnd.google-apps.folder

  1. Find a file by type such as ppt, image, etc

Use mimeType i.e image/jpeg

Google drive search filters                                                                                 View in Fusebit
// Search all files and folders by date
const dateFilter = new Date('January 01, 2022').toISOString();

// 1. Search all files and folders by date
const filesFilteredByDate = await axios.get('https://www.googleapis.com/drive/v3/files', {
    params: {
     q: `createdTime >= '${dateFilter}' or modifiedTime >= '${dateFilter}'`,
     fields: 'files(id,name,modifiedTime,createdTime,mimeType,size)',
     spaces: 'drive',
    },
    headers: {
      authorization: `Bearer ${access_token}`
    }
  });

// 2. Find a file by size
const sizeInBytes = 1024;
const filesFilteredBySize = filesFilteredByDate.data.files.filter(file => Number(file.size || 0) >= sizeInBytes);

// 3. Find all empty folders
const emptyFoldersSearch = await axios.get('https://www.googleapis.com/drive/v3/files', {
    params: {
      q: `mimeType = 'application/vnd.google-apps.folder'`,
      fields: 'files(id, name)',
      spaces: 'drive',
    },
    headers: {
      authorization: `Bearer ${access_token}`
    }
  });

const emptyFolders = [];

for await (const folder of emptyFoldersSearch.data.files) {
   const childrenResponse = await axios.get('https://www.googleapis.com/drive/v3/files', {
      params: {
        folderId: folder.id,
        spaces: 'drive',
      },
      headers: {
        authorization: `Bearer ${googleClient.fusebit.credentials.access_token}`
      }
    });

  if (!childrenResponse.data.files.length) {
    emptyFolders.push(folder);
  }
}

// 4. Find a file by type such as ppt, image, etc
const mimeType = 'image/jpeg';
const filesFilteredByType = await axios.get('https://www.googleapis.com/drive/v3/files', {
    params:{
      q: `mimeType:'${mimeType}'`,
      fields: 'files(id,name,mimeType,size)',
      spaces: 'drive',
    },
    headers: {
      authorization: `Bearer ${access_token}`
  }
});

console.log(`Found ${filesFilteredByDate.data.files.length} files/folders created or modified at ${dateFilter}`);
console.log(`Files larger than ${sizeInBytes} bytes:`, filesFilteredBySize);
console.log(`Found ${emptyFolders.length} empty folders`);
console.log(`Found ${filesFilteredByType.data.files.length} images of type ${mimeType}'`);

like image 140
Ruben Restrepo Avatar answered Oct 26 '25 14:10

Ruben Restrepo


Axios is not your problem the API does not support what you are trying to do for the most part.

Search all files and folders by date

You cant really. Download them all and search locally

Find a file by size

You cant. Download them all and search locally

Find all empty folders

Its not easy and will be a lot of requests but. You could use q to only get folders and then make separate requests using parents and then see if there are any files returned.

Find a file by type such as ppt, image, etc

Us the q parameter and search by mime type you will just need to google the correct mime type for the file you are looking for.

mimeType='application/vnd.ms-powerpoint'

You should have a look at the Search terms refrence documentation it will show you wnat you can search on.

like image 30
DaImTo Avatar answered Oct 26 '25 15:10

DaImTo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!