Tyrrrz / Tyrrrz/YoutubeExplode

Unable to download a private video with cookies (`401 Unauthorized`)

Open
#781 14 comments 0 reactions 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.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
  • 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

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

Reproduce the failure using the WebView2 cookies passed to new YoutubeClient(cookies), then compare the successful Playlists.GetAsync and GetVideosAsync calls with the failing video download request. Start at the download path after playlist enumeration; done means an accessible private video downloads without a 401 response.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.