How to Upload Files to Google Drive from WinForms C# .NET 9.0 using Google Cloud Console.
In the previous 'sixth' post ➡️ OneDrive File Upload, we looked at sharing files to Microsoft OneDrive App Folder using Microsoft OneDrive Api 'Microsoft Graph SDK'.
In this tutorial, we’ll walk through how our GSMArena scraper 📜 built with WinForms C# .NET 9.0 can upload files directly to Google Drive using the Google Cloud Console. This integration is useful for cloud backups, file sharing, and ensuring scraped data is accessible from anywhere.
1. Preparing Google Drive & API Project
To allow our GSMArena Scraper to upload files directly to Google Drive, we first need to prepare an API project in Google Cloud Console.
- Create or log into your Google Cloud account.
- Navigate to APIs & Services → Library and enable Google Drive API.
- Go to APIs & Services → Credentials and click Create Credentials → OAuth Client ID.
- Configure the OAuth Consent Screen (set application name, user type, and add your test Gmail account).
- Download the
client_secret.json
file – this contains the credentials we’ll use in our C# project.
2. Adding Google Drive .NET SDK
Open your scraper project in Visual Studio 2022 (targeting .NET 9.0). Install the official Google Drive .NET Client library from NuGet:
Install-Package Google.Apis.Drive.v3
Install-Package Google.Apis.Auth
This will give us access to the authentication flow and Drive upload methods.
3. Implementing File Upload
Now we integrate Google Drive upload inside our WinForms scraper. The snippet below shows how to authenticate and upload a file:
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System.IO;
using System.Threading;
private void UploadToGoogleDrive(string filePath)
{
string[] Scopes = { DriveService.Scope.DriveFile };
string ApplicationName = "GSMArena Scraper Uploader";
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.FromStream(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = Path.GetFileName(filePath)
};
FilesResource.CreateMediaUpload request;
using (var stream = new FileStream(filePath, FileMode.Open))
{
request = service.Files.Create(fileMetadata, stream, "application/octet-stream");
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
MessageBox.Show("Uploaded to Google Drive, File ID: " + file.Id);
}
In the code above:
client_secret.json
comes from your Google Cloud Console setup.- On first run, Google will open a browser window asking you to log in and grant permission.
- After that, the
token.json
file will be reused so the user doesn’t need to log in every time. - GoogleDriveUploader.cs file Csharp Github -https://github.com/facebookegypt/GSMArena_Csharp_Web_Data_Scraper/blob/main/clsGsmar/CouldUpload/GoogleDriveUploader.cs
4. Testing the Integration
Build and run the project, then trigger your upload method. If everything is configured correctly, your scraper’s exported data (CSV, Excel, JSON, etc.) will now be saved directly to your Google Drive.
5. GitHub Repository
The full source code is available in our GitHub repository:
📦 GSMArena Mobile Brands
GitHub Repository Showcase
This repository contains structured data for GSM Arena mobile brands, ideal for apps and web scrapers. Clean, JSON-formatted brand lists ready to use in your projects.
⭐ View on GitHub♥ Here are some online Visual Basic lessons and courses:
No comments:
Post a Comment