尝试列出非根 Google 驱动器文件夹中的文件时获取 Google.GoogleApiException 'File not found: [404]'
Get Google.GoogleApiException 'File not found: [404]' when trying to list files in a non-root Google Drive folder
我正在尝试访问 Google 驱动器中的文件。我从 the Google docs...
中获取了创建 Google 云端硬盘服务的代码
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 驱动器 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 文件夹确实存在...
有人知道我如何列出其他文件夹的内容吗?谢谢
fileList.Q = "mimeType != 'application/vnd.google-apps.folder' and 'Admin' in parents";
您遇到的问题是您使用的是目录名称'Admin'您需要使用管理目录的文件 ID。
执行 file.list 并搜索管理目录获取其文件 ID,然后将其传递给该目录。
fileList.Q = "name ='Admin' and mimeType != 'application/vnd.google-apps.folder'";
我正在尝试访问 Google 驱动器中的文件。我从 the Google docs...
中获取了创建 Google 云端硬盘服务的代码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 驱动器 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 文件夹确实存在...
有人知道我如何列出其他文件夹的内容吗?谢谢
fileList.Q = "mimeType != 'application/vnd.google-apps.folder' and 'Admin' in parents";
您遇到的问题是您使用的是目录名称'Admin'您需要使用管理目录的文件 ID。
执行 file.list 并搜索管理目录获取其文件 ID,然后将其传递给该目录。
fileList.Q = "name ='Admin' and mimeType != 'application/vnd.google-apps.folder'";