Unable to download a private video with cookies (`400 Bad Request`)
Version
6.5
Platform
.NET 9 / Windows 11
Steps to reproduce
Summary
I want to download private videos after having the Youtube cookie file.
I tried to check with the yt-dlp tool and the video can still be downloaded normally.
yt-dlp --cookies "youtube.com_cookies.txt" url
I solved it by reading the content from the cookie file and initializing the Youtube Client to download the video but got an error
Details
Details
- Parse cookie file
private static List<Cookie> ParseCookieFile(string cookieFilePath)
{
var lines = File.ReadAllLines(cookieFilePath);
return (from line in lines
where !string.IsNullOrWhiteSpace(line) && !line.StartsWith('#')
select line.Split('\t')
into parts
where parts.Length >= 7
select new Cookie
{
Domain = parts[0],
Path = parts[2],
Secure = bool.Parse(parts[3]),
Name = parts[5],
Value = parts[6]
}).ToList();
}
- Initialize youtube client with cookies
public async Task DownloadPrivateVideoAsync(string videoUrl, FileFormat fileFormat, string cookieFilePath, CancellationToken cancellationToken = default)
{
var cookies = ParseCookieFile(cookieFilePath);
var youtube = new YoutubeClient(cookies);
var request = new YtdlpRequest
{
ClassName = "Private",
FileFormat = fileFormat,
LessonName = "Private",
LessonNo = 0,
StartTrim = 0,
EndTrim = 0,
Url = videoUrl
};
var audioFileName = await DownloadMediaV2Async(youtube, request, Constants.Folder.Videos, cancellationToken).ConfigureAwait(false);
if (fileFormat == FileFormat.OnlyVideo) audioFileName.DeleteFile();
}
- Get video information
private async Task<string> DownloadMediaV2Async(YoutubeClient youtube, YtdlpRequest request, string outputPath, CancellationToken cancellationToken = default)
{
Video video = null!;
StreamManifest streamManifest = null!;
await AnsiConsoleHelper.LiveStatusAsync(async _ =>
{
var videoUrl = VideoId.Parse(request.Url);
video = await youtube.Videos.GetAsync(videoUrl, cancellationToken).ConfigureAwait(false);
streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoUrl, cancellationToken);
}, "[darkorange]Getting video info...[/]");
...
return audioFileName;
}
After getting the video information, the following code gives an error.
streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoUrl, cancellationToken);
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
- [ ] I have not read any of the above and just checked all the boxes to submit the issue
Are you getting status code 400 or actually 401?
Same issue here with version 6.5.3. 400 bad request when using cookies. are there any specific cookies that I'm supposed to use? or just copy all of them?
The 400 Bad Request error occurs due to the "clientName" and "clientVersion" fields in the GetPlayerResponseAsync function. To resolve this issue, you need to extract "clientName" and "clientVersion" from the HTML within the GetVideoWatchPageAsync function and use them in the GetPlayerResponseAsync function.