【问题标题】:Access Google Drive API Returns Empty File List访问 Google Drive API 返回空文件列表
【发布时间】:2012-10-02 20:32:57
【问题描述】:

我正在测试 Google Drive API。测试脚本执行 HTTP GET 来访问 Files->List API,但不知何故,我总是在项目中得到一个空列表。我的谷歌驱动器中有数百个文件,所以结果出乎意料。 HTTP 响应如下所示。

    {
     "kind": "drive#fileList",
     "etag": "\"VEU5fga3uYlZRf0pG-N1kS4iWP4/Kxo5eGvPKynWfroe9v5L5T43-n0\"",
     "selfLink": "https://www.googleapis.com/drive/v2/files?maxResults=5",
     "items": []
    }

【问题讨论】:

标签: google-drive-api


【解决方案1】:

要允许服务帐户访问我自己的 Google 云端硬盘中的文件,我必须向服务帐户的电子邮件地址授予权限。正确设置权限后,列表中将填充允许服务帐户访问的文件。

【讨论】:

  • 您能否详细说明您是如何授予服务帐户权限的?我与服务帐户电子邮件地址 (xxxxxxxxxx@developer.gserviceaccount.com) 共享了我的文件和文件夹,但似乎不起作用。
  • 我找到了我的问题。我将范围设置为“.../auth/drive.file”。将其更改为“.../auth/drive”可以解决问题。
  • @ifunk 我有同样的问题,你能解释一下你到底做了什么吗?提前致谢!
  • 为什么他们的文档中没有明确说明这一点?所以......很多......小时......浪费了,谢谢。
  • 我在我的 gmail 帐户中创建了一个新文件夹。然后与服务帐户共享该文件夹。它有效
【解决方案2】:

这通常意味着您已使用不允许您查看这些文件的范围进行身份验证。

例如,https://www.googleapis.com/auth/drive.file 范围仅提供对应用创建或打开的文件的访问权限。您将无法访问在 Google Drive 界面或其他应用中创建的文件。

如果您想访问所有文件,则必须使用另一个范围。例如,https://www.googleapis.com/auth/drive 为您提供访问所有用户文件的完整、许可范围

您可以找到有关可用范围here 的信息。使用它为您的应用选择合适的范围。

【讨论】:

    【解决方案3】:

    Google 并未在其文档中明确说明。经过半天的尝试修改所有可能有帮助的内容后,我发现您需要将文件与您的服务帐户 json 文件中描述的电子邮件共享。

    例如,我的个人 Google Drive 中有一个 Google Slide 文件,我希望它可以被 Google Drive API 读取,我必须与 json 服务帐户文件中列出的 xxx@xxx.iam.gserviceaccount.com 共享这个 Slide 文件.

    那么下面的代码 sn-p 应该返回共享的幻灯片文件:

    import {google} from 'googleapis'
    const credentials = require("./credentials.json");
    
    async function main() {
        const scopes = [
            'https://www.googleapis.com/auth/drive',
            'https://www.googleapis.com/auth/presentations',
        ]
    
        const auth = new google.auth.JWT(
            credentials.client_email,
            null,
            credentials.private_key,
            scopes,
        )
    
    
        const drive = google.drive({
            version: 'v3',
            auth
        })
    
        const res = await drive.files.list()
        console.log(res.data)
    
    }
    
    main()
    
    
    {
      kind: 'drive#fileList',
      incompleteSearch: false,
      files: [
        {
          kind: 'drive#file',
          id: 'xxxxx',
          name: 'xxxxx',
          mimeType: 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
        },
      ]
    }
    
    

    【讨论】:

      【解决方案4】:

      包含https://www.googleapis.com/auth/drive.readonly.metadata 范围来列出文件就足够了

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多