List Google Drive Folders and Files
using
VB.NET, WinForms, and WebView2
🔎 Overview
Want to access and display your Google Drive contents inside a VB.NET WinForms app? This post shows you how to combine the power of WebView2 and Google Drive’s web UI to list folders and files effortlessly — without direct Google API integration or billing.
This is a great solution for experimental projects or offline/local automation use.
⚙️ Requirements
- Visual Studio 2017 or newer
- .NET Framework 4.6.1+
- Windows 10 (64-bit) or higher
- WebView2 Runtime Installed
- Microsoft.Web.WebView2 NuGet Package
👇WebView installation
Download and install Microsoft Edge WebView2 Runtime on your development machine, if you haven't already.
Add the WebView2 NuGet package to your project:
Open your project in Visual Studio 2017.
Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
Search for
Microsoft.Web.WebView2
and install the latest compatible version.
🧱 Project Setup
Create a New WinForms Project
Open Visual Studio and create a new Windows Forms App (.NET Framework) project in Visual Basic.
Drag and drop a WebView2 control from the toolbox onto your form. Resize it to take up most of the form.
Go to the .
Create a new project or select an existing one.
Enable the Google Drive API for your project:
Navigate to APIs & Services → Library, search for "Google Drive API," and enable it.
Create OAuth 2.0 credentials:
Go to APIs & Services → Credentials.
Click Create Credentials → OAuth 2.0 Client IDs.
Set the application type to Desktop App.
Download the JSON file with your client ID and secret.
🙇♀️Enable Google Drive API
🔐Authentication with Google
Use the WebView2 control to handle the OAuth 2.0 authentication flow.
Write a method to load the Google login page in WebView2:
🧪 Sample Code: Load and Parse Google Drive
Method to load Google login page in WebView2
- Google requires you to send your app's credentials, scopes (permissions you're requesting), and the redirect URI (where Google sends you back after login) to the authorization endpoint.
The URL looks like this:
https://accounts.google.com/o/oauth2/auth?response_type=code&
client_id=YOUR_CLIENT_ID&
redirect_uri=http://localhost&
scope=https://www.googleapis.com/auth/drive.readonly
- Replace YOUR_CLIENT_ID with the client ID from your Google API Console.
Load the URL in WebView2:
In your code, you need to direct WebView2 to navigate to this URL when the form loads. For example:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim clientId As String = "YOUR_CLIENT_ID"
Dim redirectUri As String = "http://localhost"
Dim scope As String = "https://www.googleapis.com/auth/drive.readonly"
Dim authUrl As String = $"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id={clientId}&redirect_uri={redirectUri}&scope={scope}"
WebView21.Source = New Uri(authUrl)
End Sub
- When you run the project, the WebView2 control will navigate to the Google login page. The user will log in and authorize your app.
⁉️Handle the Authorization Code
-
Add an event handler to capture the authorization code from the redirected URL:
Private Sub WebView21_NavigationCompleted(sender As Object, e As Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs) Handles WebView21.NavigationCompleted
Dim uri As Uri = New Uri(WebView21.Source.ToString())
If uri.AbsoluteUri.Contains("code=") Then
Dim code As String = HttpUtility.ParseQueryString(uri.Query).Get("code")
' Exchange this code for an access token.
GetAccessToken(code)
End If
End Sub
📌 Explanation
- WebView2 navigates to Google Drive's UI.
- JavaScript extracts folder/file tooltips using Drive's DOM.
- ExecuteScriptAsync allows VB.NET to run this JS and get results.
- ListBox1 displays the folder/file names retrieved.
This approach uses Google’s public web UI, avoiding API limits and tokens — great for read-only listing.
💽Exchange Authorization Code for Access Token
- Use the
HttpClient
class to exchange the authorization code for an access token:
Private Async Sub GetAccessToken(code As String)
Dim httpClient As New HttpClient()
Dim postData As New Dictionary(Of String, String) From {
{"code", code},
{"client_id", "YOUR_CLIENT_ID"},
{"client_secret", "YOUR_CLIENT_SECRET"},
{"redirect_uri", "http://localhost"},
{"grant_type", "authorization_code"}
}
Dim content As New FormUrlEncodedContent(postData)
Dim response As HttpResponseMessage = Await httpClient.PostAsync("https://oauth2.googleapis.com/token", content)
Dim jsonResponse As String = Await response.Content.ReadAsStringAsync()
' Parse the JSON response to extract the access token.
End Sub
👇List Files and Folders from Google Drive
- Use the access token to call the Google Drive API and list files/folders:
Private Async Sub ListFiles(accessToken As String)
Dim httpClient As New HttpClient()
httpClient.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", accessToken)
Dim response As HttpResponseMessage = Await httpClient.GetAsync("https://www.googleapis.com/drive/v3/files")
Dim jsonResponse As String = Await response.Content.ReadAsStringAsync()
' Parse the JSON response and display the files/folders in a ListBox or DataGridView.
End Sub
🔬Run and Test
Run your project, authenticate using your Google account, and list files/folders from your Google Drive.
If HttpUtility generated an error, add reference to system.web.dll and [Imports System.Web]
🧹 Limitations
- You must be logged into Google Drive in the embedded browser.
- DOM structure may change in future updates to Google Drive.
- No advanced metadata (e.g., MIME types, file IDs) is returned.
💡 Tips
- To list Shared Drives:
https://drive.google.com/drive/shared-drives
- For Recent Files:
https://drive.google.com/drive/recent
🚀 Use Case Ideas
- Build a simple Drive browser in VB .NET.
- Automate tasks like downloading or organizing files.
- Combine with a local database to track and tag Drive contents.
♥ Here are some online Visual Basic lessons and courses:
No comments:
Post a Comment