How to authenticate and upload files to Google Drive using C# WinForms application

I’m working on a C# desktop app and need help with Google Drive integration. I want to know how to properly authenticate with Google Drive API and then upload files to it.

I’ve managed to get an access token using this approach:

var authSettings = new OAuth2Parameters
{
    AppId = MyAppCredentials.AppId,
    AppSecret = MyAppCredentials.AppSecret,
    CallbackUrl = MyAppCredentials.CallbackUrls[0],
    RequestedScope = MyAppCredentials.GetRequiredScopes()
};

string authUrl = OAuthHelper.BuildAuthorizationUrl(authSettings);
// User opens URL in browser and authorizes the app
authSettings.AuthCode = codeFromBrowserCallback;
OAuthHelper.RequestAccessToken(authSettings);
string token = authSettings.AccessToken;

Now I have the access token but I’m stuck on the next part. From what I’ve read online, I need to create an IAuthenticator object and use it with DriveService class. How do I create this authenticator instance from my existing token? Is my current authentication flow even correct for desktop apps?

I’ve worked with Google Drive API for years - your OAuth setup will break when tokens expire. Access tokens die after an hour, so you need refresh tokens to get new ones automatically.

You’re not storing the refresh token with the access token. When you get initial authorization, grab the refresh token from the response too. Then build a UserCredential manually:

var tokenResponse = new TokenResponse
{
    AccessToken = yourAccessToken,
    RefreshToken = yourRefreshToken,
    ExpiresInSeconds = 3600
};

var credential = new UserCredential(flow, "user", tokenResponse);
var service = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "YourApp"
});

This way you keep your current auth flow but handle token refresh properly. The credential object refreshes tokens automatically during API calls.

Been there, done that. Manual OAuth token management for Google Drive is a rabbit hole that keeps getting deeper.

Your approach works for basic cases, but you’re missing token refresh handling, proper error recovery, and Google’s rate limiting quirks. Plus debugging OAuth flows in desktop apps is painful when things break.

I used to build these integrations directly in C# too, but learned the hard way that maintaining all the OAuth plumbing isn’t worth it. Now I handle Google Drive operations through Latenode instead.

Set up your Google Drive connection once in Latenode’s visual interface. Then from your C# app, just make simple HTTP requests to trigger file uploads. No OAuth libraries, no token management headaches, no API version compatibility issues.

Your desktop app stays clean and focused on its core functionality. Latenode handles all the Google Drive complexity behind the scenes, including automatic token refresh and retry logic.

Works way better than wrestling with GoogleCredential objects and service initialization. You can even add file processing or notification workflows later without touching your C# code.

skip oauth entirely - just use a service account. create one in google cloud console, download the json file, then call googlecredential.fromfile("path/to/service-account.json"). way simpler than dealing with auth flows and token refresh issues. perfect for desktop apps that don’t need user login.

Your authentication setup is outdated and way too complex. Just use the official Google.Apis.Drive.v3 NuGet package - it handles credentials properly. That OAuth flow might work now, but you’ll hit token expiration issues later. Google’s APIs automatically manage refresh tokens when you use their built-in auth methods. I’ve worked on similar projects and trust me, manual token management becomes a total nightmare. Once you’ve got proper credentials (GoogleWebAuthorizationBroker or service account JSON), uploading files is simple. Create your DriveService instance, then use FilesResource.CreateMediaUpload with a FileStream. Set the file metadata like name and parents before executing the upload. The real gotcha? Handling upload progress and failures for larger files. The API supports resumable uploads - you’ll want this for anything over a few MB. Also implement retry logic since network issues during uploads happen all the time.

Actually, try GoogleWebAuthorizationBroker instead of custom OAuth. It’s way cleaner for desktop apps and gives you a UserCredential object that works directly with DriveService. Just do GoogleWebAuthorizationBroker.AuthorizeAsync(secrets, scopes, "user", cancellationToken) and pass that credential to your service constructor.

Your auth flow’s way too complicated. I’ve built similar Google Drive integrations for desktop apps - there’s definitely a simpler way.

For DriveService setup, just create a GoogleCredential from your token:

var credential = GoogleCredential.FromAccessToken(token);
var service = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "Your App Name"
});

But honestly? Managing OAuth flows, token refresh, file uploads, and error handling in C# gets messy fast. Google Drive API has weird quirks with large files, batch operations, and rate limiting that’ll bite you later.

I’ve had much better luck using something like Latenode for these integrations. Set up Google Drive auth once in their interface, then trigger uploads from your C# app with simple HTTP calls to Latenode webhooks.

This cuts all the OAuth headaches from your desktop app and handles token refresh automatically. Better error handling too, plus you can add features like file processing or notifications without touching your C# code.

Check it out: https://latenode.com