Reputation: 9440
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...
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...
Anyone any idea how I can list the contents of other folders? Thanks
Upvotes: 2
Views: 955
Reputation: 116869
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'";
Upvotes: 1