javascript - 如何使用 axios 将 api 请求设置为 google drive api

标签 javascript reactjs axios google-api google-drive-api

我从事谷歌驱动项目,但我无法使用 Axios 将搜索参数发送到谷歌 API
从前端 react
任何人都可以向我解释如何在 google drive api url 中编写查询

  • 按日期搜索所有文件和文件夹
  • 按大小查找文件
  • 查找所有空文件夹
  • 按类型查找文件,如 ppt、图片等

  • 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
  • 按类型查找文件,如 ppt、图片等

  • 使用 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/

    相关文章:

    JavaScript - 如何添加 2 个图像的 alpha 以使结果 alpha = 1?

    javascript - 是否有允许多个系列/列的 D3 仪表?

    javascript - 将 React 中的组件渲染为变量与 jsx 组件

    javascript - not.toEqual() 返回 true 尽管键值不同

    reactjs - 我如何 `npm link` 与对等依赖项的 typescript 依赖项?

    javascript - AWS S3 使用预签名 URL 更新镜像(Axios-PUT 请求)

    javascript - Javascript POST 上的 NS_Error_Failure

    javascript - 当用户取消选择 div 时停止代码运行

    c# - Axios.Delete() 不执行然后两个 catch block

    javascript - 服务 png 图像的 Axios 图像损坏