Click here to Skip to main content
13,298,481 members (57,862 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 29 Jul 2014

Sample VB.NET Program using Google APIs for .NET V3 with YouTube

, 26 Jul 2016
Rate this:
Please Sign up or sign in to vote.
Provides a sample program using Google APIs for .NET V3 with YouTube


This sample program demonstrates how to use several of the Google APIs with YouTube. It does not demonstrate every API. Some of the techniques demonstrated in this sample program can be used with other Google API Client Library for .NET V3 classes.

Some of the Google APIs Client Library for .NET V3 classes demonstrated in this sample program are as follows:

  • GoogleWebAuthorizationBroker.AuthorizeAsync
  • ChannelsResource.ListRequest
  • VideoCategoriesResource.ListRequest
  • VideosResource.InsertMediaUpload.UploadAsync
  • VideosResource.InsertMediaUpload.ResumeAsync
  • VideosResource.ListRequest
  • VideosResource.DeleteRequest


Because some of the Google APIs Client Library for .NET operate asynchronously (i.e., separate threads), it is important to understand how to interact with Windows Forms Controls since cross-thread access is not allowed.

This sample was developed using Visual Studio 2013.

Using the Code

Installing the Source Code and Libraries

  1. Before attempting to use this sample program, ensure that all Microsoft Windows Update patches have been installed.
  2. Obtain a client secret json file by registering on the Google Developers Console at
  3. After opening the solution for the sample program in Visual Studio, there will be a lot of errors in Visual Studio's Error List window. This is because the Google APIs Client Library for .NET NuGet packages have not been distributed with this sample program.

    To install the Google APIs Client Library for .NET in Visual Studio NuGet packages, click on Tools / NuGet Package Manager / Package Manager Console and enter the following command:

    Install-Package Google.Apis.YouTube.v3
  4. After the NuGet package install is complete, click Tools / NuGet Package Manager / Manage NuGet Packages for Solution, then click on Updates / and install all of the update packages.
  5. Edit the GetCredentials subroutine to insert your ClientId and ClientSecret. These are found in the client secret json file. This sample program shows how to embed the ClientId and ClientSecret in the source code. There are other samples provided by Google that show how to read ClientId and ClientSecret from the client secret json file.

Executing the Sample Program

Using the program to demonstrate the APIs consists of six steps.

  1. Select File: Using a OpenFile dialog window, select the video file to be uploaded.
  2. Get Credentials: Using OAUTH2 protocol, obtain a token file and then retrieve the YouTube channel and channel description. The first time GoogleWebAuthorizationBroker.AuthorizeAsync is called, a browser window is instantiated and you are prompted for the username and password of your YouTube channel. Upon entering the correct username and password, a token file is downloaded to the C:\Users\<YourWindowsUserName>\AppData\Roaming\Google.Apis.Auth directory. Subsequent calls will refresh the token file if necessary.
  3. Get Categories: Retrieve the list of video categories, insert each category id and category title in a custom class and add the custom class to the Item list of a ComboBox.
  4. Upload Video: Select the category and enter the video's title, description, keywords, date recorded and privacy status and then upload the video to YouTube. Sample code is provided to resume the upload in case of a network server issue. I know that resume works when "A task was canceled" error is returned after an HTTPClient TimeOut error. After every "chunk size" bytes is uploaded, the ProgressChanged event is fired. In the sample program, when there is a whole percent difference between the last percent complete value and the current complete value, a percent complete line is added to the ListBox. In this sample program, the "chunk size" is set to .5MB. After a video is successfully uploaded, the Video Id is assigned to the Video Id TextBox.
  5. Status: The video's status is retrieved, formatted and displayed in a TextBox. The sample code that retrieves the video information is designed to retrieve information for multiple videos. In this sample, only the information for the most recently uploaded video is retrieved.
  6. Delete Video: Deletes the most recently uploaded video.

Points of Interest

Windows Forms Controls are not supposed to be directly accessed across threads. This sample program shows how to add items to a ListBox and assign a value to a TextBox from separate threads. The Microsoft examples show how to use a control's InvokeRequired property to determine if BeginInvoke/EndInvoke are required for cross-thread access to a control. This sample program does not use the InvokeRequired property. Instead, it always uses BeginInvoke/EndInvoke when I believed that a control would be accessed in a cross-thread environment.


  • Version 1: 29 July 2014 - Initial version
  • Version 2: 21 April 2016
    • In IsResumable(), added a test for "A task was canceled" error message to allow retry after an HTTPClient TimeOut error.
    • Before .UploadAsync, set objYouTubeService.HttpClient.Timeout to a value higher than the default.
    • In .ResumeAsync Do..Loop, added Thread.Sleep() delay using ExponentialBackoff.GetNextBackoff.
    • In .ResumeAsync Do..Loop, inserted Thread.Sleep(3000) delay to ensure ProgressChanged Event has time to execute and to allow extra time for network and/or server issues to resolve.
    • In .ResumeAsync Do..Loop, inserted a condition to quit after a specified number of retries.
    • Added Try..Catch blocks around .UploadAsync and .ResumeAsync. Exception handling and reporting is done in the ProgressChanged event.
  • Version 3: 6 May 2016
    • Modifed the IsResumable() function so that it always returns True.
  • Version 4: 11 July 2016

    • Updated to .NET Framework 4.5.1.
    • Updated to Google API .NET Client version 1.14.0 (Released 5 July 2016).
    • Added code to demonstrate a new feature of Google .NET Client API: Ability to resume an upload after a program restart.
    • Modified IsResumable() function to test for number of retries and CancellationToken.IsCancellationRequested.
    • Modified UploadStatus.Failed case of the videosInsertRequest_ProgressChanged event handler to set retries to max value if the error contains Google.Apis.Requests.RequestError

    Version 5: 26 July 2016

    • Modified UploadStatus.Failed case of the videosInsertRequest_ProgressChanged event to improve HTTP Status error code handling on failed upload.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mike Meinz
United States United States
I’m retired. When I started my career, programming projects consisted of plugging wires into plug boards to create punch card processing applications to be run on electrical accounting machine like the IBM 402, 407, 085, 088, 514, 519, etc. From there, I moved to writing SPS and Autocoder applications on an IBM 1401 with 4K of memory eventually upgraded to 16K of memory. After many years of migrating my skills to various languages on various hardware platforms, I became an Information Technology Director where I didn’t need to program anymore. So, starting in 1996, I volunteered my time with a local community cable television organization and built some applications to help them run their operations. Originally in Clipper Summer 1987 and later Clipper 5.2, I migrated and enhanced those applications to VB .NET 2003 in 2003. I retired from my full-time job in 2010. Since then, I have continued to support the local community cable tv organization's applications. In 2013, I migrated the VB .NET 2003 Solution to VB .NET 2012 so that it can run on 64-bit computers and interact with Microsoft Office 2010. The upgrade went smoothly. In mid 2013, I developed a VB .NET 2012 application for them to download election results data from the Secretary of State's web site, format the results and send them to a VizRT character generator for on-air display.

You may also be interested in...


Comments and Discussions

QuestionHow to Upload Thumbnail for Uploaded Video Pin
ahgsql10-Apr-17 16:10
memberahgsql10-Apr-17 16:10 
QuestionVideo updaterequest Pin
maxthegoof12-Apr-16 12:53
membermaxthegoof12-Apr-16 12:53 
AnswerRe: Video updaterequest Pin
Mike Meinz13-Apr-16 6:09
memberMike Meinz13-Apr-16 6:09 
GeneralRe: Video updaterequest Pin
maxthegoof13-Apr-16 11:29
membermaxthegoof13-Apr-16 11:29 
QuestionHorrible Documentation of Instructions... Pin
Member 1197834411-Sep-15 21:14
memberMember 1197834411-Sep-15 21:14 
QuestionHangs on first time Creds Pin
Ageng Dwi Prastyawan4-Jul-15 23:47
memberAgeng Dwi Prastyawan4-Jul-15 23:47 
QuestionFirst time Oauth2 Authentication Pin
tlydon22-Oct-14 5:07
membertlydon22-Oct-14 5:07 
AnswerRe: First time Oauth2 Authentication Pin
Mike Meinz22-Oct-14 7:29
memberMike Meinz22-Oct-14 7:29 
QuestionHangs on GetCredentials Pin
Geving17-Sep-14 12:04
memberGeving17-Sep-14 12:04 
AnswerRe: Hangs on GetCredentials Pin
Mike Meinz17-Sep-14 14:34
memberMike Meinz17-Sep-14 14:34 
GeneralRe: Hangs on GetCredentials Pin
Geving21-Sep-14 10:59
memberGeving21-Sep-14 10:59 
GeneralRe: Hangs on GetCredentials Pin
Mike Meinz21-Sep-14 11:42
memberMike Meinz21-Sep-14 11:42 
QuestionSample download Pin
Avelino Ferreira18-Aug-14 5:53
memberAvelino Ferreira18-Aug-14 5:53 
AnswerRe: Sample download Pin
Mike Meinz18-Aug-14 5:58
memberMike Meinz18-Aug-14 5:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 26 Jul 2016
Article Copyright 2014 by Mike Meinz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid