VB .NET Google Drive Api Source Code Example


VB .NET Google Drive Api Source Code Example

VB .NET Google Drive Api Source Code Example
VB .NET Google Drive Api Source Code Example

Requirements

Visual Basic 2012 [recommended VS 2015] or above (Visual Studio 2019) from here
Visual Basic previous Example from here
Google Account from here
.NET Framework 4.5+ or above. "Async Programming"
Google Drive Api [Google.Apis.Drive.v3] from NuGet here

Open VB 2015 Project in VB 2019 

Can I open / migrate Visual Basic 2015 Project Source Code with Visual Basic 2019 ?
The answer is Yes, if you want to more details about VB 2015 to VB 2019 migration, please read

VB.NET Google Drive Api v3 post key points

Project description

I will use the VB.NET example from previous post [Backup Database to Google Drive] to add another functionality to it. I will add Backup Database to Google Drive [ToolStripMenuItem]
VB .NET Google Drive Api Source Code Example
VB .NET Google Drive Api Source Code Example
When a user press [ToolStripMenuItem -> Backup to Google Drive] : The VB .NET application does the following :
  • Checks for Internet Connection Status.
  • Connects to Google Drive project created in Google developer section [App Console].
  • Uploads the Microsoft Access 2007 (*.accdb) Database file from previous post to remote folder [/Backups] created manually by me in my Google Drive account.
  • Returns a string [File uploaded to Google Drive] to verify [UploadFile] method.

Installing Google Drive Api (Google.Apis.Drive.v3) from Visual Studio 2015

To install latest Google.Apis.Drive.v3 from Visual Basic 2015 :

VB .NET Google Drive Api installation using NuGet in VS2015
VB .NET Google Drive Api installation using NuGet in VS2015

  • In the console manager in Visual Studio, paste the PM Code and press <Enter>
VB .NET Google.Apis.Drive.v3 installation using NuGet in VS2015
VB .NET Google.Apis.Drive.v3 installation using NuGet in VS2015
  • Wait for Google.Apis.Drive.v3 download to 100% complete and restart Visual Studio 2015, to ensure that you Successfully installed 'Google.Apis.Drive.v3' to [HierarchicalTreeView] project.
  • Now, in your Visual Basic 2015 Solution Explorer panel [References] would looks something like this :
VB .NET Google.Apis.Drive.v3 installation using NuGet in VS2015 Reference
VB .NET Google.Apis.Drive.v3 installation using NuGet in VS2015 Reference

Create Google Console Project for .NET to use Google Drive Api.

VB .NET Google Api Console Create new project
Google Api Console Create new project

VB .NET Google Drive Api Project Overview
Google Drive Api Project Overview
In the above picture, make sure you Google Drive Api is present in the bottom of the page

Using OAuth 2.0 to Access Google APIs

As stated in Google Developer Guide :
Installed applications 
The Google OAuth 2.0 endpoint supports applications that are installed on devices such as computers, mobile devices, and tablets. When you create a client ID through the Google API Console, specify that this is an Installed application, then select Android, Chrome App, iOS, or "Other" as the application type. The process results in a client ID and, in some cases, a client secret, which you embed in the source code of your application. (In this context, the client secret is obviously not treated as a secret.) The authorization sequence begins when your application redirects a browser to a Google URL; the URL includes query parameters that indicate the type of access being requested. Google handles the user authentication, session selection, and user consent. The result is an authorization code, which the application can exchange for an access token and a refresh token. The application should store the refresh token for future use and use the access token to access a Google API. Once the access token expires, the application uses the refresh token to obtain a new one.
 For more details (Oauth for Mobile and Desktop Apps)

OAuth 2.0 for Desktop applications 'Installed Applications' ?

As stated here (https://developers.google.com/identity/protocols/oauth2/native-app) 'OAuth 2.0 Desktop Apps' : 
This authorization flow is similar to the one used for web server applications. The main difference is that installed apps must open the system browser and supply a local redirect URI to handle responses from Google's authorization server.

What do we need from Google Console Api ?

VB.NET Google Console Api - Drive Api
VB.NET Google Console Api - Drive Api

    • In order to start coding your application you need some data from Google Console Api to use in your VB.NET project :
You need to create OAuth 2.0 client IDs from Credentials section. For installed applications (like desktop applications) you will not need to specify any information in the OAuth consent screen only the application name and your Google email address.
Your Google Console Api Credentials Section


  1. Application Name (Backup-DB-Desktop) in the top of the page.
  2. Client ID (something.apps.googleusercontent.com)
  3. Client Secret (Found when you click on OAuth Name)

VB.NET Google Drive Api Desktop Project Code

  • We create a Service using those credentials above
  • We upload the file
  • Check your Google Drive account associated with your original Google Account Email.
  • I used a small file to upload.
  • I will also include "Google Drive Resumable Upload" ChunkedUpload() method in my source code example for my 13mb Access database file.

VB.NET Google Drive Api Desktop Project Run

When you run the application for the first time, you will go through some security issues like :
  • Google will prompt you to verify access from another application
  • Refresh AccessToken is done via your browser, so you will notice that your browser will open for refreshing accesstoken
  • Those would occur only once.

Try the QuickStart example first, this example is designed only for testing.


vb.net google drive api v3 OAuth 2.0 Quick Start Example Permission
vb.net google drive api v3 OAuth 2.0 Quick Start Example Permission

vb.net google drive api v3 OAuth 2.0 allow access
vb.net google drive api v3 OAuth 2.0 allow access

vb.net google drive api v3 OAuth 2.0 received verification code received

VB.NET Google Drive Api v3 OAuth2 vs Service Account

Authentication is necessary if you want to access Google Drive API. There are 2 types of Authentications:
  1. OAuth2 : Will allow you to access another users data, 
  2. Service Account : Can be set up to access your own data.

VB.NET Google Drive Api v3 QuickStart dotnet example source code


VB.NET Google Drive API TroubleShooting

Error

VB.NET Google Drive API Error (At least one client secrets (Installed or Web) should be set)

Diagnostic :

You are getting this error because you are trying to use UserCredential instead of GoogleCredential. In the Quick Start Example for Google Drive API Dotnet :

Solution :

VB.NET

Dim Credential As UserCredential

C#

UserCredential credential;

Change the above code to :

VB.NET

Dim Credential as GoogleCredential

C#

GoogleCredential Credential;


Password : projectzone.co
Github repo for the Google Drive Api v3 QuickStart dotnet example for Visual Basic from here

Link to this post

https://adonetaccess2003.blogspot.com/2019/09/vb-net-google-drive-api-source-code.html

Update


  • I have found and converted to VB.NET, the C# Resumable Upload for large files uploading.
  • I've uploaded an example VB.NET Source code on uploading large files to Google Drive using the v3 Api.


Password projectzone.co

Popular posts from this blog

Visual Basic Online Courses DataGridView Add Edit Delete

VB .NET WebView2 WinForms tips

VB NET Check internet connection

Visual Basic 2010 Working With DataBase Full Project Example

Visual Basic 2010 Math Functions