查找与我共享的文件夹中的所有文件 Google 驱动器 API v3

Find All Files in Folder Shared With Me Google Drive API v3

我正在编写一个 .NET C# 程序来使用来自 Google 驱动器的文件。

我有几个文件夹与我共享。其中一个文件夹中有一些我需要在我的应用程序中使用的文件。

我可以获取共享目录OK:

// Get a list of files in the Share with me folder
var request = CurrentGDriveService.Files.List();
request.Q = "(sharedWithMe = true)";
request.Fields = "*";
var results = request.Execute();

results.Files 仅显示共享文件夹,以及这些文件夹下的 none 个文件。

虽然我无法通过 Drive api 看到文件夹中的文件,但当我通过浏览器通过 Google Drive 登录时,我可以完全访问这些文件。

其他有帮助的代码...

身份验证

static string[] Scopes = { DriveService.Scope.Drive };
static string ApplicationName = "Drive API .NET Interface";

using (var stream =
       new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
  {
    string TokenCredentialsFullPath = "token.json";
    credentials = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        Scopes,
                        "user",
                        System.Threading.CancellationToken.None,
                        new FileDataStore(TokenCredentialsFullPath, true)).Result;
  }

连接到服务:

// Create Drive API service.
CurrentGDriveService = new DriveService(new 
     BaseClientService.Initializer()
      {
         HttpClientInitializer = usrCredentials,
         ApplicationName = ApplicationName,
      });

像对待 IList 一样对待响应

FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 10;
listRequest.Fields = "nextPageToken, files(id, name)";
listRequest.Corpora = "allDrives";
listRequest.Q = "sharedWithMe = true"; // not in brackets!

// List files.
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files;

foreach (var file in files)
{
    Console.WriteLine("{0} ({1})", file.Name, file.Id);
}

这会打印出 10 个与您共享的文件及其 ID。

参考