Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get Google.GoogleApiException 'File not found: [404]' when trying to list files in a non-root Google Drive folder

I’m trying to access files in Google Drive. I picked up code to create a Google Drive service from the Google docs

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;
}

Using code found in this blog post, I can make a call to the Google Drive API and see all the files in the root folder…

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...
}

That works fine, but if I try to list the files in a folder…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

…then when it tries to make the call to fileList.Execute() I get the following exception…

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

As you can see, the Admin folder does exist…

enter image description here

Anyone any idea how I can list the contents of other folders? Thanks

>Solution :

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

The issue you are having is that you are using the name of the directory ‘Admin’ you need to use the file id of the Admin directory.

Do a file.list and search for the admin directory get its file id then pass it to that instead.

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading