Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Upload a file to YouTube. Console application.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
namespace YouTubeExampleUpload
{
class Program
{
public static readonly string PathToCredentials = @"C:\YouTube\dev\credentials.json";
public static readonly string UploadFileName = "TestVideo.mkv";
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
var userCredentials = await ConfigureUserCredential();
var service = CreateService(userCredentials);
var video = new Video
{
Snippet = new VideoSnippet
{
Title = "Test video",
Description = "Upload dummy video"
}
};
video.Status = new VideoStatus {PrivacyStatus = "unlisted"};
var videoUploader = new UploadVideo(service);
await videoUploader.Upload(video, UploadFileName);
}
private static async Task<UserCredential> ConfigureUserCredential()
{
// Credential storage location
var tokenStorage = new FileDataStore("UserCredentialStoragePath", true);
UserCredential credential;
await using var stream = new FileStream(PathToCredentials, FileMode.Open, FileAccess.Read);
// Requesting Authentication or loading previously stored authentication for userName
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
(await GoogleClientSecrets.FromStreamAsync(stream)).Secrets,
new[] {YouTubeService.ScopeConstants.Youtube},
"userName",
CancellationToken.None,
tokenStorage)
.Result;
return credential;
}
private static YouTubeService CreateService(UserCredential credential)
{
return new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
}
}
}
using System;
using System.IO;
using System.Threading.Tasks;
using Google.Apis.Upload;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
namespace YouTubeExampleUpload
{
public class UploadVideo
{
private readonly YouTubeService _service;
public UploadVideo(YouTubeService service)
{
_service = service;
}
public async Task Upload(Video video, string uploadFileName)
{
using (var fileStream = new FileStream(uploadFileName, FileMode.Open))
{
var videosInsertRequest = _service.Videos.Insert(video, "snippet,status", fileStream, "video/*");
videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
await videosInsertRequest.UploadAsync();
}
}
public void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
switch (progress.Status)
{
case UploadStatus.Uploading:
Console.WriteLine("{0} bytes sent.", progress.BytesSent);
break;
case UploadStatus.Failed:
Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
break;
}
}
public void videosInsertRequest_ResponseReceived(Video video)
{
Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment