如何在 CefSharp 浏览器的 Windows 表单中使用 YouTube API

How to use YouTube API in a Windows Form for CefSharp Browser

我正在尝试用 CefSharp 制作一个网络浏览器。我还试图让它有一个 YouTube 内容的网络拦截器,主要是为了学习 YouTube API 的工作原理。由于关于 C# 的 API 的文档很少,我对应该如何执行此操作感到困惑。我的代码位于 GitHub。我的 API 代码在 Video.cs 和 Form1.cs 中。这是 Video.cs 的代码:

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;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Tools
{
    public class YouTubeServiceClient
    {
        private static YouTubeServiceClient instance;
        private Video video;

        public static YouTubeServiceClient Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new YouTubeServiceClient();
                }
                return instance;
            }
        }

        public Video Video { get => video; set => video = value; }

        public async Task Run(string videoId)
        {
            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 for read-only access to the authenticated 
                    // user's account, but not other types of account access.
                    new[] { YouTubeService.Scope.YoutubeReadonly },
                    "user",
                    CancellationToken.None,
                    new FileDataStore(this.GetType().ToString())
                );
            }

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

            var request = youtubeService.Videos.List("snippet");
            request.Id = videoId;

            // Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
            var response = await request.ExecuteAsync();
            if (response.Items.Count == 1)
            {
                Video = response.Items[0];
            }
        }
    }
}

我使用了它并收到了回复,将我发送到 Chrome 以输入我的凭据。 在此之后我使用了我的 Form1.cs,这里是处理它的片段:

private async void OnBrowserAddressChanged(object sender, AddressChangedEventArgs e)
        {
            this.InvokeOnUiThreadIfRequired(() => searchBar.Text = e.Address);
            if (youtubeBlocker && Regex.IsMatch(e.Address, StringTools.WildCardToRegular("https://www.youtube.com/watch*")))
            {
                Uri addressUrl = new Uri(e.Address);
                string videoId = HttpUtility.ParseQueryString(addressUrl.Query).Get("v");

                MessageBox.Show(videoId);
                YouTubeServiceClient videoClient = new YouTubeServiceClient();

                try
                {
                    await videoClient.Run(videoId);
                }
                catch
                {
                    MessageBox.Show("FAILED");
                }

                foreach (string word in blacklistedWords)
                {
                    if (Regex.IsMatch(videoClient.Video.Snippet.Description.ToLower(), StringTools.WildCardToRegular(word)))
                    {
                        MessageBox.Show("BLACKLISTED");
                        break;
                    }
                }

            }
        }

虽然我转到了正确的 YouTube 页面,但我的 BLACKLISTED 消息没有弹出。我真的很困惑为什么会这样,并且已经研究了 2 天。我的代码有问题吗?我需要使用其他东西吗?有没有更好的办法?谢谢。

编辑: 我不断收到这些错误:

[0227/083150.786:INFO:CONSOLE(0)] "The resource https://r8---sn-a8au-hp5l.googlevideo.com/generate_204 was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it wasn't preloaded for nothing.", source: https://www.youtube.com/watch?v=Ebp8T1HfiLk (0)

[0227/083150.789:INFO:CONSOLE(0)] "The resource https://r8---sn-a8au-hp5l.googlevideo.com/generate_204?conn2 was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it wasn't preloaded for nothing.", source: https://www.youtube.com/watch?v=Ebp8T1HfiLk (0)

原来我找错地方了。处理描述值的代码是错误的。如果您想了解我是如何使用 YouTube Explode 的,以及我是如何修复它的,请查看上面的 GitHub 页面。抱歉,如果这是一个愚蠢的问题,那是我不明白如何使用 contains 方法。抱歉,谢谢。