Tyrrrz / Tyrrrz/YoutubeExplode

Unable to download a private video with cookies (`400 Bad Request`)

Open
#852 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug help wanted
Dominant language
C#
Stars
3.7k
Forks
592
Avg merge
3h 4m
Merged PRs (30d)
1

Description

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
  1. 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();
    }

image

  1. 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();
}
  1. 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;
 }

image

After getting the video information, the following code gives an error.
streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoUrl, cancellationToken);

image

image

Checklist
  • I have looked through existing issues to make sure that this bug has not been reported before
  • I have provided a descriptive title for this issue
  • I have made sure that this bug is reproducible on the latest version of the package
  • 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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at YoutubeClient(cookies) and the failing Videos.Streams.GetManifestAsync(videoUrl, cancellationToken) call, then compare its cookie handling with the supplied yt-dlp behavior. Reproduce the 400 response using the shown cookie-file parsing and private video flow. Done means a private video manifest can be retrieved with the cookie file without the bad request.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, authentication
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.