How to retrieve directory list using Google Drive API service account

I’m trying to fetch directories from a user’s Google Drive using a service account but getting no results back. The API connection works fine since I can get account information successfully. My setup includes proper API enablement and service account credentials but the folder query returns empty. Here’s my implementation:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;

namespace DriveManager.Core.Handlers;

public class DriveApiHandler
{
    private DriveService _service;

    public DriveApiHandler()
    {
        var keyPath = "credentials-98bbcf33401b.json";
        GoogleCredential auth = GoogleCredential.FromFile(keyPath)
                                               .CreateScoped([DriveService.Scope.Drive,
                                                             DriveService.Scope.DriveFile,
                                                             DriveService.Scope.DriveReadonly]);

        var config = new BaseClientService.Initializer { HttpClientInitializer = auth };
        _service = new DriveService(config);
    }

    public List<Google.Apis.Drive.v3.Data.File> FetchDirectories()
    {
        var query = _service.Files.List();
        query.IncludeItemsFromAllDrives = true;
        query.SupportsAllDrives = true;
        query.Q = "mimeType='application/vnd.google-apps.folder'";

        var response = query.Execute();
        return response.Files.ToList();
    }
}

Testing the connection works fine:

var testReq = _service.About.Get();
testReq.Fields = "*";
var info = testReq.Execute();

What could be causing the empty folder results?

Yeah, this is definitely a permissions issue. Your service account can’t access anything unless you explicitly share it with the service account email (you’ll find this in your JSON file). Share a test folder with that email first to make sure everything’s working.

This looks like a permissions issue, not a problem with your code. Service accounts can only see files that are explicitly shared with them or files they created themselves - they can’t access someone else’s personal Drive files. Since you’re trying to fetch directories from a user’s Google Drive, the service account doesn’t have access. If you need to access specific user files, you’ve got two options: Set up domain-wide delegation (if you’re on G Suite/Workspace) - this lets the service account impersonate users in your organization, but needs admin approval or switch to OAuth 2.0 user authentication instead of service account auth. If you’re working with shared/team drives, make sure your service account is added as a member with the right permissions. Your code looks fine for querying folders - the empty results just mean you’re hitting a permissions wall, not a technical bug.

Your service account code is correct, but you are encountering an access limitation. Service accounts can only access files they own or that have been explicitly shared with them, which is why your account retrieval works but folder queries return empty. To resolve this, manually share a folder from the user’s Google Drive with the service account’s email found in your credentials JSON for testing purposes. For production use, consider either setting up domain-wide delegation if you’re using Google Workspace to impersonate users or use OAuth2 for user consent. This restriction is a security measure to prevent unauthorized access to user data.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.