0
votes

I am trying to do an application to live broadcast some videos using youtube. The idea is to play some videos in my application, and if I want, live broadcasting them using youtube.

For now, I am just trying to run a simple code sample, to upload a video. The sample is from this web-site: https://developers.google.com/youtube/v3/code_samples/dotnet#upload_a_video

This is the code I have:

Program.cs

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        VideoStreamTest video = new VideoStreamTest();
    }
}

VideoStreamTest.cs

With the following usings:

using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

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

And my class:

class VideoStreamTest
{
    public VideoStreamTest()
    {
        Console.WriteLine("=================\nTest Broadcast Youtube\n=================");
        init();
    }

    private static void init()
    {
        Console.WriteLine("YouTube Data API: Upload Video");
        Console.WriteLine("==============================");

        try
        {
            new UploadVideo().Run().Wait();
        }
        catch (AggregateException ex)
        {
            foreach (var e in ex.InnerExceptions)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }

        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }

    private async Task Run()
    {
        UserCredential credential;
        using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                // This OAuth 2.0 access scope allows an application to upload files to the
                // authenticated user's YouTube channel, but doesn't allow other types of access.
                new[] { YouTubeService.Scope.YoutubeUpload },
                "user",
                CancellationToken.None
            );
        }

        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
        });

        var video = new Video();
        video.Snippet = new VideoSnippet();
        video.Snippet.Title = "Default Video Title";
        video.Snippet.Description = "Default Video Description";
        video.Snippet.Tags = new string[] { "tag1", "tag2" };
        video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
        video.Status = new VideoStatus();
        video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
        var filePath = @"REPLACE_ME.mp4"; // 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;

            await videosInsertRequest.UploadAsync();
        }
    }

    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;
        }
    }

    void videosInsertRequest_ResponseReceived(Video video)
    {
        Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
    }
}

I have installed the following NuGet Packages:

Google.Apis.YouTube.v3

Google.Apis

But when I run my code I have the following error:

VideoStreamTest.cs(31,13,31,24): error CS0246: The type or namespace name 'UploadVideo' could not be found (are you missing a using directive or an assembly reference?)

What am I doing wrong?

2

2 Answers

1
votes

In addition to the "using" statements in the source code file, you have to add references to the API assemblies in the References area of the project.

0
votes

In my case, When I copy & paste 'using Google.Apis.YouTube.v3;' an error occurred, But typing directly with a keyboard the error disappear.