I’m building an application that needs to retrieve folder information from a user’s Google Drive account using C# and OAuth2 authentication. While I have successfully implemented file uploading functionality, I’m struggling with fetching the directory structure.
I tried using the DocumentsService approach but keep running into authentication issues. Here’s my current implementation:
string APP_ID = "my_app_id";
string APP_SECRET = "my_secret_key";
var authClient = new NativeApplicationClient(GoogleAuthenticationServer.Description, APP_ID, APP_SECRET);
var authResult = SetupAuthentication(authClient);
DocumentsService driveService = new DocumentsService("my_project");
driveService.SetAuthenticationToken(authResult.RefreshToken);
FolderQuery folderRequest = new FolderQuery();
DocumentsFeed results = driveService.Query(folderRequest); // Error occurs here
foreach (DocumentEntry item in results.Entries)
{
string folderName = item.Title.Text;
Console.WriteLine(folderName);
}
And my authentication helper method:
private static IAuthorizationState SetupAuthentication(NativeApplicationClient authClient)
{
const string CACHE_ID = "drive_cache";
const string AUTH_KEY = "auth_token";
string apiScope = "drive_scope_url";
IAuthorizationState authState = AuthorizationMgr.GetCachedRefreshToken(CACHE_ID, AUTH_KEY);
if (authState != null)
{
try
{
authClient.RefreshToken(authState);
return authState;
}
catch (Exception)
{
// Token refresh failed
}
}
authState = AuthorizationMgr.RequestNativeAuthorization(authClient, apiScope);
AuthorizationMgr.SetCachedRefreshToken(CACHE_ID, AUTH_KEY, authState);
return authState;
}
I keep getting 401 Unauthorized errors when trying to query for folders. I also attempted using OAuth2Authenticator but the State property remains null. What’s the correct way to authenticate and retrieve folder listings from Google Drive?
Your DocumentsService is from the old Google Documents List API that got shut down in 2012. That’s why authentication keeps failing - Google doesn’t accept requests to that endpoint anymore. You’ll need to switch to Google Drive API v3 with the modern client libraries. Install the Google.Apis.Drive.v3 NuGet package and ditch your current auth setup for GoogleWebAuthorizationBroker. For desktop apps, use GoogleWebAuthorizationBroker.AuthorizeAsync() - it handles OAuth2 properly and gives you a UserCredential object. Then create your DriveService with that credential. To list folders, use Files.List() with a query parameter like “mimeType=‘application/vnd.google-apps.folder’” to filter folders only. The response structure is totally different from what you’re expecting - Drive API v3 treats folders as files with specific MIME types. Set your OAuth scope to “https://www.googleapis.com/auth/drive.readonly” or something broader if you need more access.
you’re using a dead api - google killed off documentsservice ages ago. switch to drive api v3, it’s way cleaner. also, your oauth setup is way too complicated. just use googlewebauthorizationbroker.authorizeasync for desktop apps and let it handle the token management automatically.
Yeah, everyone’s talking about the deprecated API, but there’s a way better solution. Skip the whole Google client library and OAuth mess - just automate it.
I dealt with this exact problem on a project that synced folders across multiple Drive accounts. Writing auth code and handling token refresh? Total nightmare.
What actually worked was an automation workflow that handles the Drive connection, auth, and folder listing automatically. Your C# app just sends a simple HTTP request and gets back clean JSON with the folder data.
The automation platform deals with all the OAuth crap, token management, and API versioning. Your C# code becomes dead simple - make a REST call, parse the response. Done.
I’ve got one running folder syncs every few hours that pushes results to our main app. Never breaks, and I don’t have to think about Google’s auth flows anymore.
Latenode is solid for this - connects to Drive natively and you can build workflows for exactly what you need: https://latenode.com
Had this exact headache migrating legacy Drive code last year. Google killed that DocumentsService approach around 2012 - that’s why you’re getting those 401 errors. Your auth logic looks fine, but you’re hitting dead endpoints.
Here’s what fixed it for me: install the Google.Apis.Drive.v3 package and rewrite the folder stuff from scratch. The new DriveService takes a BaseClientService.Initializer with your creds. For OAuth2, just use GoogleWebAuthorizationBroker.AuthorizeAsync - it handles token refresh automatically.
For folders, you’ll call service.Files.List() with q=“mimeType=‘application/vnd.google-apps.folder’” since v3 treats folders like special files instead of separate things. The response is totally different too - you get File objects with a Name property, not DocumentEntry with Title.Text. Your current auth helper won’t work with the new API either.
Your code uses the old Google Documents List API that’s been dead for years. That’s why you’re getting auth errors - Google ditched support for Drive operations through that API ages ago. You need to switch to Google Drive API v3 with the current .NET client library. Grab the Google.Apis.Drive.v3 NuGet package and swap DocumentsService for DriveService. The auth flow’s different too - use GoogleAuthorizationCodeFlow with UserCredential for OAuth2. I hit the same wall migrating old code and discovered the new API needs different scopes. Double-check you’re requesting the right Drive API scopes in your OAuth setup. Your folder querying code won’t work either - you’ll have to rewrite it using Files.List() with query parameters to filter folders only.