【发布时间】: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