Google `drive.files.list` 不列出文件。

huangapple go评论130阅读模式
英文:

Google `drive.files.list` doesn't list files

问题

我遇到了Google Drive API的问题。我传递了文件夹ID,但没有显示任何文件。我应该如何遍历子文件夹以检查其中的文件和文件夹?

const path = require('path')
const { JWT } = require('google-auth-library')
const { google } = require('googleapis')
const axios = require('axios')

// 获取授权令牌
const getAuth = ({ email, key }) => {
  const scopes = ['https://www.googleapis.com/auth/drive']
  return new JWT({
    email,
    key,
    scopes,
  })
}

async function loadDrive(options) {
  const { folderId, key, service_email } = options
  const auth = await getAuth({ email: service_email, key })

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

  try {
    const res = await drive.files.list({
      pageSize: 10,
      fields: 'nextPageToken, files(id, name)',
      q: `'${folderId}' in parents`,
      spaces: 'drive',
    })

    const files = res.data.files
    if (files.length === 0) {
      console.log('未找到文件。')
    } else {
      console.log('文件:')
      for (const file of files) {
        console.log(`${file.name}${file.id})`)
      }
    }
  } catch (e) {
    console.log(e)
  }
}

请注意,这是您提供的代码的翻译版本。如果您有任何其他问题或需要进一步的帮助,请告诉我。

英文:

I'm having issues with the Google drive API. I am passing in the folder ID and it's not showing any files. How do I traverse within the sub folders to check for folders and files there?

const path = require('path')
const { JWT } = require('google-auth-library')
const { google } = require('googleapis')
const axios = require('axios')

// Get auth token
const getAuth = ({ email, key }) => {
  const scopes = ['https://www.googleapis.com/auth/drive']
  return new JWT({
    email,
    key,
    scopes,
  })
}

async function loadDrive(options) {
  const { folderId, key, service_email } = options
  const auth = await getAuth({ email: service_email, key })

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

  try {
    const res = await drive.files.list({
      pageSize: 10,
      fields: 'nextPageToken, files(id, name)',
      q: `'${folderId}' in parents`,
      spaces: 'drive',
    })

    const files = res.data.files
    if (files.length === 0) {
      console.log('No files found.')
    } else {
      console.log('Files:')
      for (const file of files) {
        console.log(`${file.name} (${file.id})`)
      }
    }
  } catch (e) {
    console.log(e)
  }
}

答案1

得分: 1

[Update 01/04/22]: 我在 https://console.cloud.google.com/iam-admin/serviceaccounts 下分享了我的服务帐户电子邮件,与 Google Drive 中的文件夹共享,并能够访问 Drive 数据。

Google `drive.files.list` 不列出文件。

英文:

[Update 01/04/22]: I shared my service account email under https://console.cloud.google.com/iam-admin/serviceaccounts with the folder in Google drive and was able access the Drive data.

Google `drive.files.list` 不列出文件。

huangapple
  • 本文由 发表于 2023年1月4日 19:33:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75004812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定