I am trying to make a web browser in CefSharp. I am also trying to make it have a web blocker for YouTube content, mostly for learning purposes on how the YouTube API works. Since there is so little documentation on the API for C#, I am confused on how I am supposed to do this. My code is located at GitHub. My code for the API is in Video.cs and Form1.cs. Here is the code for 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];
}
}
}
}
I used it and got a response sending me to Chrome to enter in my credentials. After this I used my Form1.cs, here is the snippet that deals with it:
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;
}
}
}
}
Although I go to the right YouTube pages, my BLACKLISTED message does not pop up. I am really confused to why this might be, and been working on it for 2 days. Is there something wrong with my code? Do I need to use something else? Is there a better way? Thank you.
EDIT: I keep on getting these errors:
[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)