Search code examples
c#google-drive-apigoogle-drive-shared-drive

Google DriveService Files.List() not returning results


Edit:

I've tried granting the SA access to my personal drive (within the organization Workspace) to do some troubleshooting. After granting rights to the SA to a particular folder and rewriting the code to examine that folder, it successfully returned information about files within the test folder. The conclusion is the SA has been set-up correctly by our IT department and does have adequate scope and rights to read files in our organizations Workspace. So, the questions remain: why can't it return information about files in a Shared Drive? What other parameters need to be set in order to get it to return those files? Are there entirely other functions that need to be used? I did notice the deprecated TeamDrives.List() function, but the guidance when trying to use it was to use Files.List() as I had written originally.

--- end edit ---

We have a Google Workspace environment. I've been granted a Service Account (SA) by our IT department and am trying to use it to help maintain access rights. The SA has been granted Content Manager rights to a shared drive instance. I've tried following along this YouTube tutorial. In stepping through the code execution, it appears to log in correctly, but it is not returning any files. I've tried substituting the full URL for the file ID of the root folder I'd like to examine, but then it returns a 404 error, so I think it is finding the correct folder. If the file ID is used the code runs without errors, it simply returns no files (and there are hundreds of folders and files within the root). Any suggestions?

namespace DriveQuickstart

{ class Program

{

    static string[] Scopes = { DriveService.Scope.DriveReadonly };
    private const string PathToServiceAccountKeyFile = @"<path to jason Service Account file>";
    private const string ServiceAccountEmail = @"<Service Account "email">";
            static void Main(string[] args)
    {
        MainAsync().Wait();
    }
    static async Task MainAsync()
    {
        var credential = GoogleCredential.FromFile(PathToServiceAccountKeyFile)
                        .CreateScoped(new[] { DriveService.ScopeConstants.Drive });
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential
        });
        var request = service.Files.List();
        request.IncludeItemsFromAllDrives = true;
        request.SupportsAllDrives = true;
        request.Q = "parents in '<id of "root" folder in shared drive>'";
        FileList results = await request.ExecuteAsync();

        foreach (var driveFile in results.Files)
        {
            Console.WriteLine($"{driveFile.Name}  {driveFile.MimeType}  {driveFile.Id}");
        }
    }
}

}


Solution

  • OK, it appears the @DAIMTO example is specific to personal drives. The Q() parameter syntax is incorrect for Team drives in the example. To make it work in Team environment:

    • IncludeItemsFromAllDrives parameter must be set to true

    • SupportsAllDrives parameter must be set to true

    • the Q search parameter syntax for finding specific directories is:

      Q = "'folder_ID' in parents and mimeType = 'application/vnd.google-apps.folder'"; -- or mimeType of your choice

    (note: this is reversed from the youtube example of "parents in 'folder_ID'")