Unable to download a private video with cookies (`401 Unauthorized`)
Version
6.3.13
Platform
.NET 8.0 / Window 11
Steps to reproduce
Step 1
- Get all the cookie from web browser using WebView2
List<CoreWebView2Cookie> wv2Cookies = [];
if (webView.CoreWebView2 != null)
wv2Cookies = await webView.CoreWebView2.CookieManager.GetCookiesAsync("https://youtube.com");
CookieCollection sysNetCookieCollection = [];
wv2Cookies.ForEach(c => sysNetCookieCollection.Add(c.ToSystemNetCookie()));
var postData = new DownloadMultiPlaylistsRq
{
Cookies = sysNetCookieCollection.ToImmutableList(),
PlaylistUrls = [
"https://www.youtube.com/playlist?list=PLgomaFin7XDmN6yMSDH6vQ5vWBWsbFB0K"
//"https://www.youtube.com/playlist?list=PLgomaFin7XDmVk3B20G9WtCxervsc1tt0"
]
};
string json = Newtonsoft.Json.JsonConvert.SerializeObject(postData);
// Prepare the content
var content = new StringContent(json, Encoding.UTF8, "application/json");
// Send POST request
HttpResponseMessage response = await httpClient.PostAsync("https://localhost:7151/api/v1/VideoDownload/playlists", content);
I use WPF for get all the cookie then pass it to my dotnet 8.0 API.
Step 2
- Init
var youtubeClient2 = new YoutubeClient(cookies);
Step 2
- Getting all videos from a playlist that contain private videos that I can access with my youtube account. All the videos are returned.
- Get each private video info is okay but when download I got 401.
An unhandled exception has occurred while executing the request. System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
public async Task<BaseRpModel> DownloadMultiPlaylistsAsync(IReadOnlyList<Cookie> cookies, List<string> playlistUrls, CancellationToken cancellationToken = default)
{
if (!Directory.Exists(setting.VideoPath)) Directory.CreateDirectory(setting.VideoPath);
foreach (var playlistUrl in playlistUrls)
{
await DownloadPlaylistAsync(cookies, playlistUrl, cancellationToken);
}
return new BaseRpModel
{
Code = "OK",
Message = "Done downloading ... ",
};
}
public async Task<BaseRpModel> DownloadPlaylistAsync(IReadOnlyList<Cookie> cookies, string playlistUrl, CancellationToken cancellationToken = default)
{
if (!Directory.Exists(setting.VideoPath)) Directory.CreateDirectory(setting.VideoPath);
var youtubeClient2 = new YoutubeClient(cookies);
var playlist = await youtubeClient2.Playlists.GetAsync(playlistUrl, cancellationToken);
var playlistPath = Path.Combine(setting.VideoPath, playlist.Title);
Directory.CreateDirectory(playlistPath);
var videos = await youtubeClient2.Playlists.GetVideosAsync(playlist.Id, cancellationToken);
logger.LogDebug("Total videos: {@total}", videos.Count);
//var tasks = videos.Select(i => RunAsync(i, youtubeClient2, playlistPath, cancellationToken));
//await Task.WhenAll(tasks);
foreach (var i in videos)
{
await RunAsync(i, youtubeClient2, playlistPath, cancellationToken);
}
return new BaseRpModel
{
Code = "OK",
Message = "Done downloading ... ",
};
}
Details
- Expectation: I think it should not have any from problem with download private video coz I have all the cookies
- Actual Result:
An unhandled exception has occurred while executing the request. System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized)..
Thank you so much for check and help this out.🙏🙏
Checklist
- [X] I have looked through existing issues to make sure that this bug has not been reported before
- [X] I have provided a descriptive title for this issue
- [X] I have made sure that this bug is reproducible on the latest version of the package
- [X] I have provided all the information needed to reproduce this bug as efficiently as possible
- [ ] I have sponsored this project
Is this still reproducible with the latest version?
If I had to guess, there might be an issue with (de-)serialization of cookies when sending them to your backend. However, if you can fetch the private videos in a playlist, it's unlikely for that to be the issue.
You can also try fetching and downloading just one video and see if it changes anything.
Is this still reproducible with the latest version?
If I had to guess, there might be an issue with (de-)serialization of cookies when sending them to your backend. However, if you can fetch the private videos in a playlist, it's unlikely for that to be the issue.
You can also try fetching and downloading just one video and see if it changes anything.
Hi @Tyrrrz thank you for reply.
I will try it again.
The problem exists in version 6.4.3. My code:
public Form1()
{
InitializeComponent();
}
private async void Form1_Load(object sender, EventArgs e)
{
await webView21.EnsureCoreWebView2Async();
}
private void Button1_Click(object sender, EventArgs e)
{
webView21.CoreWebView2.Navigate(@"https://accounts.google.com/ServiceLogin?continue=http://www.youtube.com");
}
private async void Button2_Click(object sender, EventArgs e)
{
try
{
List<CoreWebView2Cookie> coreWebView2Cookies = await webView21.CoreWebView2.CookieManager.GetCookiesAsync("");
List<Cookie> cookies = new List<Cookie>();
foreach (CoreWebView2Cookie item in coreWebView2Cookies)
{
cookies.Add(item.ToSystemNetCookie());
}
YoutubeClient youtube = new YoutubeClient(cookies);
string videoUrl = "https://www.youtube.com/watch?v=c9DIoSNoQNs";
StreamManifest streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoUrl);
IStreamInfo streamInfo = streamManifest.GetVideoOnlyStreams().GetWithHighestVideoQuality();
await youtube.Videos.Streams.DownloadAsync(streamInfo, $"video.{streamInfo.Container}");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I get an error in the line
StreamManifest streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoUrl);
Screenshot of the error:
Just putting this out there, Looking at the requests on a browser the hash is different from the what in the library.
I changed YoutubeHttpHandler.cs line 68 to this:
return $"SAPISIDHASH {timestamp}_{tokenHash}_u";
Instead of getting un-authorized I now get bad-request. Hope this helps move this issue forward.
Yes, it looks like something changed in how YouTube verifies authentication cookies. Any help with finding a fix is appreciated.
Response status code does not indicate success: 401 (Unauthorized)
Can someone share the full stack trace? Which exact method throws this?
Full Stacktrace in VideoController.GetPlayerResponseAsync: specific line: var playerResponse = PlayerResponse.Parse( await response.Content.ReadAsStringAsync(cancellationToken) ); Message: Response status code does not indicate success: 401 (Unauthorized). No Inner Exceptions Stacktrace: at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at YoutubeExplode.Videos.VideoController.<GetPlayerResponseAsync>d__5.MoveNext() in //YoutubeExplode/Videos/VideoController.cs:line 99 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at YoutubeExplode.Videos.Streams.StreamClient.<GetStreamInfosAsync>d__8.MoveNext() in //YoutubeExplode/Videos/Streams/StreamClient.cs:line 270 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at YoutubeExplode.Videos.Streams.StreamClient.<GetManifestAsync>d__9.MoveNext() in /_/YoutubeExplode/Videos/Streams/StreamClient.cs:line 304 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Threading.Tasks.ValueTask`1.get_Result() at Callstack:
[Exception] YoutubeExplode.dll!YoutubeExplode.Videos.VideoController.GetPlayerResponseAsync(YoutubeExplode.Videos.VideoId videoId, System.Threading.CancellationToken cancellationToken) Line 99 C# [Exception] YoutubeExplode.dll!YoutubeExplode.Videos.Streams.StreamClient.GetStreamInfosAsync(YoutubeExplode.Videos.VideoId videoId, System.Threading.CancellationToken cancellationToken) Line 270 C# [Exception] YoutubeExplode.dll!YoutubeExplode.Videos.Streams.StreamClient.GetManifestAsync(YoutubeExplode.Videos.VideoId videoId, System.Threading.CancellationToken cancellationToken) Line 304 C# [Exception] System.Threading.Tasks.Extensions.dll!System.Threading.Tasks.ValueTask<TResult>.Result.get() Unknown
Thank you @flowhl
I have the exact same problem, but with doing anything with cookies (downloading, getting manifest, etc.), not just private videos. I extracted them from a cef instance, to circumvent the please sign in error, now I get this error
Even after signing in from webview2 , passing the HttpClient instance with cookies to youtubeclient, the download fails with please sign-in error , while getting the streams through YouTube.Videos.Streams.GetManifestAsync
@Tyrrrz saw this link . Eventhough it is for a different project , saw something about PO token which is being used to get rid of error with cookies. Not sure how this would be useful in youtubeexplode and youtube, but just added here
https://github.com/yt-dlp/yt-dlp/wiki/Extractors#po-token-guide
@Tyrrrz saw this link . Eventhough it is for a different project , saw something about PO token which is being used to get rid of error with cookies. Not sure how this would be useful in youtubeexplode and youtube, but just added here
Thanks @DotNet-Fan. I saw that and it looks like that would require a lot of manual effort on the user's part to extract cookies from their browser. May help with the problem that YoutubeDownloader is having (because it owns a browser window), but unfortunately is not a really good solution in the general case, so I'm still evaluating.
@Tyrrrz I did test this scenario and I was expecting that the download would work when logged in from a YouTube premium account but that's not the case. I get the same error Video 'xx_XXXxXxxX' is unplayable. Reason: 'Please sign in'
YouTube Premium allows you to download from a browser for offline purposes.