我从事谷歌驱动项目,但我无法使用 Axios 将搜索参数发送到谷歌 API
从前端 react
任何人都可以向我解释如何在 google drive api url 中编写查询
axios 请求是这样的
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)
})
提前致谢 :)
最佳答案
这是您如何使用带有 axios 的 Google Drive API 来解决您的每个问题:
是的你可以!您可以使用 创建时间 和 修改时间过滤器(查看更多可用的查询词here)
是的,您可以,但是,您需要返回文件/文件夹的大小并按特定大小过滤生成的文件,您不需要下载文件,但由于您需要获取文件,因此效率可能会有点低首先是文件(使用查询过滤器来限制结果)。
是的你可以!但与上一点类似,您需要迭代每个文件夹并使用文件夹 id 搜索文件,检查文件是否为空。
确保您仅使用 mimeType 过滤器过滤文件夹 应用程序/vnd.google-apps.folder
使用 mimeType 即 图片/jpeg
Google 云端硬盘搜索过滤器
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}'`);
关于javascript - 如何使用 axios 将 api 请求设置为 google drive api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71123422/