2
votes

i am using c#, i can upload video youtube my channel successfully. Now i want to upload video include thumbnail. I got no error in debug but the thumbnail did not upload, here is my code:

    private static async Task Upload(FileInfo Vidf)
    {
        //2.1 Get credentials
        UserCredential credentials;

        //2.1.1 Use https://console.developers.google.com/ to get the json file (Credential section)
        using (var stream = new FileStream(APIjson, FileMode.Open, FileAccess.Read))
        {
            credentials = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new[] { YouTubeService.Scope.YoutubeUpload },
                "user",
                CancellationToken.None);
        }

        //2.2 Create a YoutubeService instance using our credentials
        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credentials,
            ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
        });


        //2.3 Create a video object
        var video = new Video()
        {
            Id = Vidf.Name,
            Status = new VideoStatus
            {
                PrivacyStatus = "private"
            },
            Snippet = new VideoSnippet
            {
                Title = "...",
                Description = "...",
            }
        };

        var filePath = Vidf.FullName;
        filesize = Vidf.Length;
        //2.4 Read and insert the video in youtubeService
        using (var fileStream = new FileStream(filePath, FileMode.Open))
        {
            var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
            videosInsertRequest.ProgressChanged += ProgressChanged;
            videosInsertRequest.ResponseReceived += ResponseReceived;

            //2.4.1 Wait for the upload process
            await videosInsertRequest.UploadAsync();
        }


        var tpath = Vidf.FullName.Replace("mp4", "jpg");

        using (var tStream = new FileStream(tpath, FileMode.Open))
        {
            var tInsertRequest = youtubeService.Thumbnails.Set(video.Id, tStream, "image/jpeg");
            tInsertRequest.ProgressChanged += ProgressChanged;

            await tInsertRequest.UploadAsync();
        }

    }

Do i have to upload video first and wait finished youtube processing the video. Then upload thumbnails like example code in this link https://developers.google.com/youtube/v3/docs/thumbnails/set#examples

1
i got this error picasaweb.google.com/lh/photo/…. So this mean must upload first and wait youtube finished all task. then upload thumbnail, am i right?Sicay

1 Answers

1
votes

create a global var "VidID" and set value in ResponseReceived function

static void ResponseReceived(Video video)
    {
        VidID = video.Id;
    }

should get video ID and then post thumbnail with that video ID.