【问题标题】:C# Setting NotifySubscribers on YouTube.Videos.Insert with YouTube API v3C# 使用 YouTube API v3 在 YouTube.Videos.Insert 上设置 NotifySubscribers
【发布时间】:2017-06-30 01:07:00
【问题描述】:

我正在使用 YouTube API v3 将视频上传到 YouTube。我目前可以上传视频,更新标题和说明,但对我来说非常重要的是取消选中(布尔值 false)NotifySubscribers 属性。

我已经能够找到文档herehere,但是没有任何实现示例,我感觉很迷茫。

以下是我目前能够成功上传的代码,无需将 NotifySubscribers 设置为 false:

        private async Task Run()
        {
            UserCredential credential;
            using (var stream = new FileStream("youtube_client_secret.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 = "test";
            video.Snippet.Description = "test description";
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
            var filePath = @"D:\directory\YoutubeUploader\2017-02-05_02-01-32.mp4";
            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;
            }
        }

我原以为设置该值会像设置标题、隐私状态等一样简单,但情况似乎并非如此。我从来没有做过任何真正深入的 C# 编码,所以其中一些概念对我来说是相当新的。

【问题讨论】:

  • 尝试将隐私状态更改为公开,此forum 讨论了任何视频上传为私人/不公开可能会导致订阅者无法收到通知。如果您在每次上传时手动勾选了通知订阅者,请检查您的 YouTube 频道。默认情况下,notifySubscribers 设置为 True

标签: c# youtube youtube-api youtube-data-api


【解决方案1】:

事实证明,当前的 Google.Apis.YouTube.v3 NuGet 包 (1.21.0.760) 无法做到这一点。看起来他们在 .dll 中留下了许多方法,这导致功能非常有限。为了解决这个问题,我从this GitHub 存储库中获取了代码,并将其放入它自己的 .cs 文件中。然后我就可以调用这个文件中的所有方法。完成此操作后,我只需执行以下操作即可更改此设置:

videosInsertRequest.NotifySubscribers = false;

【讨论】:

    猜你喜欢
    • 2013-02-19
    • 2014-09-10
    • 2016-04-10
    • 2014-08-25
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 2012-11-29
    • 2017-04-15
    相关资源
    最近更新 更多