Air*_*ity 4 javascript google-api google-drive-api reactjs axios
我正在开发一个谷歌驱动器项目,但我无法使用 Axios 将搜索参数从前端 React 发送到谷歌 API
可以向我解释一下如何编写像 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)
})
Run Code Online (Sandbox Code Playgroud)
提前致谢 :)
您可以通过以下方式将 Google Drive API 与 axios 结合使用来解决您的每个问题:
\n是的你可以!您可以使用createdTime和modifiedTime过滤器(在此处查看更多可用的查询术语)
\n是的,您可以,但是,您将需要返回文件/文件夹的大小并按特定大小过滤结果文件,您不需要下载文件,但它可能有点低效,因为您需要首先获取文件(使用查询过滤器来限制结果)。
\n是的你可以!但与上一点类似,您需要迭代每个文件夹并使用文件夹 ID 搜索文件,检查文件是否为空。\n确保您仅使用 mimeType 过滤器application/vnd.google-apps.folder来过滤文件夹
\n使用 mimeType 即image/jpeg
\n| Google 云端硬盘搜索过滤器 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2 \xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2 \xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2 \xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 | View in Fusebit |
|---|
// Search all files and folders by date\nconst dateFilter = new Date(\'January 01, 2022\').toISOString();\n\n// 1. Search all files and folders by date\nconst filesFilteredByDate = await axios.get(\'https://www.googleapis.com/drive/v3/files\', {\n params: {\n q: `createdTime >= \'${dateFilter}\' or modifiedTime >= \'${dateFilter}\'`,\n fields: \'files(id,name,modifiedTime,createdTime,mimeType,size)\',\n spaces: \'drive\',\n },\n headers: {\n authorization: `Bearer ${access_token}`\n }\n });\n\n// 2. Find a file by size\nconst sizeInBytes = 1024;\nconst filesFilteredBySize = filesFilteredByDate.data.files.filter(file => Number(file.size || 0) >= sizeInBytes);\n\n// 3. Find all empty folders\nconst emptyFoldersSearch = await axios.get(\'https://www.googleapis.com/drive/v3/files\', {\n params: {\n q: `mimeType = \'application/vnd.google-apps.folder\'`,\n fields: \'files(id, name)\',\n spaces: \'drive\',\n },\n headers: {\n authorization: `Bearer ${access_token}`\n }\n });\n\nconst emptyFolders = [];\n\nfor await (const folder of emptyFoldersSearch.data.files) {\n const childrenResponse = await axios.get(\'https://www.googleapis.com/drive/v3/files\', {\n params: {\n folderId: folder.id,\n spaces: \'drive\',\n },\n headers: {\n authorization: `Bearer ${googleClient.fusebit.credentials.access_token}`\n }\n });\n\n if (!childrenResponse.data.files.length) {\n emptyFolders.push(folder);\n }\n}\n\n// 4. Find a file by type such as ppt, image, etc\nconst mimeType = \'image/jpeg\';\nconst filesFilteredByType = await axios.get(\'https://www.googleapis.com/drive/v3/files\', {\n params:{\n q: `mimeType:\'${mimeType}\'`,\n fields: \'files(id,name,mimeType,size)\',\n spaces: \'drive\',\n },\n headers: {\n authorization: `Bearer ${access_token}`\n }\n});\n\nconsole.log(`Found ${filesFilteredByDate.data.files.length} files/folders created or modified at ${dateFilter}`);\nconsole.log(`Files larger than ${sizeInBytes} bytes:`, filesFilteredBySize);\nconsole.log(`Found ${emptyFolders.length} empty folders`);\nconsole.log(`Found ${filesFilteredByType.data.files.length} images of type ${mimeType}\'`);\n\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2180 次 |
| 最近记录: |