How to upload a Youtube video automatically (C# .NET 2012)

First you need to add to your project Google.Apis.YouTube.v3 Client Library. You can do this by using NuGet, and you can find a guide at this address. Then, the code is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

using System.Threading;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
	switch (progress.Status)
	{
		case UploadStatus.Uploading:
			// log_writeline(progress.BytesSent.ToString() + " bytes sent.");
			break;

		case UploadStatus.Failed:
			// log_writeline("An error prevented the upload from completing.\n" + progress.Exception.ToString());
			break;
	}
}

void videosInsertRequest_ResponseReceived(Video video)
{
	// log_writeline("Video " + video.Snippet.Title + " was successfully uploaded.");
}
		
private async Task upload_videos()
{		
	String client_secret_path = ""; // here you put the path to your .json client secret file, generated in your Google Developer Account
	String username = ""; // youtube account login username


	UserCredential credential;
	using (var stream = new FileStream(client_secret_path, FileMode.Open, FileAccess.Read))
	{
		credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
			GoogleClientSecrets.Load(stream).Secrets,
			new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload },
			username,
			CancellationToken.None,
			new FileDataStore(this.GetType().ToString())
		);
	}

	var youtubeService = new YouTubeService(new BaseClientService.Initializer()
	{
		HttpClientInitializer = credential,
		ApplicationName = this.GetType().ToString()
	});

	var video = new Video();
	video.Snippet = new VideoSnippet();

	string title = "";
	string description = "";
	List<String> keywords = new List<String>();

	video.Snippet.Title = title;
	video.Snippet.Description = description;
	video.Snippet.Tags = new System.Collections.Generic.List<String>();


	video.Snippet.CategoryId = "27"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
	video.Status = new VideoStatus();
	video.Status.PrivacyStatus = "public"; // or "private"

	var filePath = ""; // Replace with path to actual movie file.

	using (var fileStream = new FileStream(filePath, FileMode.Open))
	{
		var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
		videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
		videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
		videosInsertRequest.NotifySubscribers = true;

		await videosInsertRequest.UploadAsync();
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close