Configuration
The YouTubeSessionCreator class can be customized using the YouTubeSessionConfig
object passed to its constructor. This page outlines all available options and how to use them effectively.
using YouTubeSessionGenerator;
YouTubeSessionConfig config = new()
{
JsEnvironment = myCustomJsEnvironment, // Required when generating Proof of Origin Tokens
HttpClient = myCustomHttpClient, // Optional: Provide your own HttpClient
Logger = myCustomLogger // Optional: Enable logging
};
JsEnvironment
To generate a Proof of Origin Token and bypass YouTube’s anti-bot system, BotGuard, you need to execute custom JavaScript code in an environment that supports the DOM (access to window
& document
).
- Type:
IJsEnvironment
- Required for
CreateProofOfOriginTokenAsync
- If omitted, methods like
CreateVisitorDataAsync
&CreateRolloutTokenAsync
will still work.
using YouTubeSessionGenerator.Js.Environments;
using NodeEnvironment nodeEnvironment = new("C:\\Program Files\\nodejs\\node.exe");
config.JsEnvironment = nodeEnvironment;
Caution
If you're passing a JsEnvironment
, you are responsible for disposing it! For example, the built-in NodeEnvironment
spawns a subprocess that won't be closed until disposed.
Use a using
statement or manually call .Dispose()
in a try/finally
block when done.
Built-in
The built-in environment uses Node.js under the hood, and supports Windows, Linux & macOS.
Why not use a .NET JavaScript interpreter?
You might be wondering: Why not use a .NET-based JavaScript engine like Jint, instead of depending on the bulkier Node.js runtime?
It’s a fair question. In theory, using something like Jint would make the library fully self-contained and cross-platform with no external dependencies.
However, generating valid Proof of Origin tokens requires more than just executing JavaScript - it requires simulating a realistic browser-like environment. Tools like AngleSharp.js try to replicate the DOM with support for realastic JavaScript execution, but sadly these projects are not quite mature yet. Another thing we can try is to port Js-libraries like jsdom or happy-dom to pure JavaScript - without the dependecies of the Node environment. Unfortunately, even after doing that, integrity tokens generated by BotGuard were still invalid and no WebPo minter was created, which is required for minting poTokens.
The problem probably lies in subtle but critical differences:
- Jint lacks a proper event loop and microtasks/macrotasks queues
- Available .NET DOM simulations aren't full-featured enough for BotGuard's fingerprinting
- Key APIs like
setTimeout
,setInterval
,performance.now()
, or Node'svm
module aren't available
After investing far too many hours trying to make it work, I ultimately opted for a small Node.js subprocess instead. It’s still much lighter than bundling a full headless browser like Chromium, and it actually works.
That said, this project is open-source - so if you can get it working with a pure .NET engine, I’d love to see a pull request :3
Custom
If you're targeting other platforms than Windows, Linux or macOS, you can implement your own JavaScript execution logic - e.g., by using a headless browser or a cloud-based JavaScript environment. To do this, you’ll need to implement the IJsEnvironment
interface:
using YouTubeSessionGenerator.Js;
public class CustomJsEnvironment : IJsEnvironment
{
Task<string?> ExecuteAsync(JsScript script);
{
string code = script.Code;
object?[] args = script.Args;
// Execute the code with the given args..
}
}
Make sure your custom environment supports the DOM and passes all tests in the test suite.
HttpClient
You can inject a shared or customized HTTP client into the YouTubeSessionCreator, e.g., for timeouts or proxy support. This client is used to send all HTTP requests to YouTube and Google servers.
- Type:
HttpClient
- if omitted, a default instance will be created.
using System.Net;
using HttpClient httpClient = new(new HttpClientHandler() { Proxy = new WebProxy("http:/‎/proxyserver:80/", true) })
{
Timeout = TimeSpan.FromSeconds(30)
};
config.HttpClient = httpClient;
Logger
YouTubeSessionGenerator uses a standard logger to output progress, debug information and error messages. It’s built on top of Microsoft.Extensions.Logging, so you can easily integrate it with any logging provider (e.g., Console, Debug, File, Serilog, etc.).
- Type:
ILogger
- if omitted, logging is simply disabled.
using Microsoft.Extensions.Logging;
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
});
config.Logger = loggerFactory.CreateLogger("YouTubeSessionGenerator");