【问题标题】:Get Google.GoogleApiException 'File not found: [404]' when trying to list files in a non-root Google Drive folder尝试列出非根 Google Drive 文件夹中的文件时获取 Google.GoogleApiException 'File not found: [404]'
【发布时间】:2021-12-22 10:42:29
【问题描述】:

我正在尝试访问 Google 云端硬盘中的文件。我从the Google docs获取了创建 Google Drive 服务的代码...

private static DriveService GetDriveService() {
  UserCredential credential;
  using (FileStream stream = new("credentials_desktop.json", FileMode.Open, FileAccess.Read)) {
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
      GoogleClientSecrets.Load(stream).Secrets, 
      Scopes, 
      "user", 
      CancellationToken.None, 
      new FileDataStore("token.json", true)).Result;
  }
  DriveService service = new(new BaseClientService.Initializer {
    HttpClientInitializer = credential,
    ApplicationName = ApplicationName,
  });
  return service;
}

使用this blog post 中的代码,我可以调用 Google Drive API 并查看根文件夹中的所有文件...

private static void ListFiles() {
  DriveService service = GetDriveService();
  FilesResource.ListRequest fileList = service.Files.List();
  fileList.Q = "mimeType != 'application/vnd.google-apps.folder' and 'root' in parents";
  fileList.Fields = "nextPageToken, files(id, name, size, mimeType)";
  List<File> files = new();
  string pageToken = null;
  do {
    fileList.PageToken = pageToken;
    FileList filesResult = fileList.Execute();
    IList<File> pageFiles = filesResult.Files;
    pageToken = filesResult.NextPageToken;
    files.AddRange(pageFiles);
  } while (pageToken != null);
  // dump files to console...
}

这很好,但如果我尝试列出文件夹中的文件...

fileList.Q = "mimeType != 'application/vnd.google-apps.folder' and 'Admin' in parents";

...然后当它尝试调用fileList.Execute() 时,我得到以下异常...

Google.GoogleApiException
  HResult=0x80131500
  Message=Google.Apis.Requests.RequestError
File not found: . [404]
Errors [
    Message[File not found: .] Location[fileId - parameter] Reason[notFound] Domain[global]
]

  Source=Google.Apis
  StackTrace:
   at Google.Apis.Requests.ClientServiceRequest`1.<ParseResponse>d__35.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Google.Apis.Requests.ClientServiceRequest`1.Execute()
   at GoogleDrivePlay.Program.ListFiles2() in d:\GoogleDrivePlay\GoogleDrivePlay\Program.cs:line 59
   at GoogleDrivePlay.Program.Main(String[] args) in d:\GoogleDrivePlay\GoogleDrivePlay\Program.cs:line 23

如您所见,Admin 文件夹确实存在...

有人知道如何列出其他文件夹的内容吗?谢谢

【问题讨论】:

    标签: c# google-drive-api google-api-dotnet-client


    【解决方案1】:
    fileList.Q = "mimeType != 'application/vnd.google-apps.folder' and 'Admin' in parents";
    

    您遇到的问题是您使用的是目录“Admin”的名称,您需要使用 Admin 目录的文件 ID。

    做一个 file.list 并搜索 admin 目录获取它的文件 id 然后将它传递给它。

    fileList.Q = "name ='Admin' and mimeType != 'application/vnd.google-apps.folder'";
    

    【讨论】:

    • 太棒了,非常感谢。可惜谷歌文档太差了,如果他们说清楚的话,我会节省很多时间。
    • Google 的文档让我有理由创建有关此类主题的 YouTube 视频。 Google Drive API first step list and find files with C#?
    • @dalmTo 感谢您的链接,希望我早点找到!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2019-11-05
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多