Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow authenticated requests via cookie values #346

Merged
merged 10 commits into from
Jul 3, 2023
Merged

Conversation

xBaank
Copy link
Contributor

@xBaank xBaank commented Jun 28, 2023

Closes #198

This pull request enables the usage of SAPISID and __Secure-3PSID cookie values to perform authenticated requests. When used, all requests will be made using the ANDROID client, resolving the issue of Occasionally streams are very slow to download for videos like rXMX4YJ7Lks and 6fFnUaeXXeg.

imagen

To get the required cookies:

  1. Go to https://www.youtube.com
  2. Login with an account
  3. Open the developer tools (F12)
  4. Go to the Storage or Application tab
  5. Go to Cookies then https://www.youtube.com and copy the SAPISID and __Secure-3PSID values

I have a few questions though:

  • Is it better to get the cookies via a file similar to yt-dl? (the cookies are exported using a plugin in the browser)
  • Is it worth including the username/password option? This approach seems more challenging as it would involve parsing web pages, dealing with captchas, challenges, TFA (Two-Factor Authentication), etc. And after all that get the cookies that the server sent.

@Tyrrrz
Copy link
Owner

Tyrrrz commented Jun 29, 2023

Hey @xBaank, thanks a lot for the PR! I think this is a cool feature, but I was thinking maybe we can automate the extraction of these cookies?

I was thinking maybe add a button you can click to show an embedded browser, have the user log in, and then extract those cookies automatically. In WPF, this should be doable with the WebBrowser control, I believe.

What do you think?

@Tyrrrz Tyrrrz added the enhancement New feature or request label Jun 29, 2023
@xBaank
Copy link
Contributor Author

xBaank commented Jun 30, 2023

I didn't think about that but it seems to be a much better approach 😁, I will take a look at it.

@Tyrrrz
Copy link
Owner

Tyrrrz commented Jun 30, 2023

I didn't think about that but it seems to be a much better approach 😁, I will take a look at it.

Really excited to see it happen 🙂

@xBaank
Copy link
Contributor Author

xBaank commented Jun 30, 2023

I wasn't able to do it with WebBrowser because google recognized it as a not secure browser, but WebView2 seems to work.
How do you see it?

imagen
imagen
imagen

YoutubeDownloader.Core/Utils/AuthHandler.cs Outdated Show resolved Hide resolved
YoutubeDownloader/Services/SettingsService.cs Outdated Show resolved Hide resolved
YoutubeDownloader/ViewModels/Dialogs/BrowserViewModel.cs Outdated Show resolved Hide resolved
YoutubeDownloader/Views/Dialogs/BrowserView.xaml.cs Outdated Show resolved Hide resolved
Comment on lines +173 to +181
<Button
Margin="16"
HorizontalAlignment="Stretch"
Command="{s:Action Logout}"
Content="Logout"
IsCancel="True"
IsDefault="True"
Visibility="{Binding IsLogged, Converter={x:Static converters:BoolToVisibilityConverter.VisibleOrCollapsed}}"
Style="{DynamicResource MaterialDesignOutlinedButton}" />
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we can display the username here? So that the user knows what account they're logged in as

Copy link
Contributor Author

@xBaank xBaank Jul 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making a request to https://www.youtube.com/youtubei/v1/account/account_menu?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8&prettyPrint=false returns accountName and channelHandle, I don't know if there is any other way to get it.
Should we go this way?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need to make a request, then it's not worth it. I was hoping there would be something available directly after logging in 🤔

@Tyrrrz
Copy link
Owner

Tyrrrz commented Jul 3, 2023

Thanks a lot @xBaank! I'll take it from here and make some adjustments

@Tyrrrz Tyrrrz merged commit f72e8d5 into Tyrrrz:master Jul 3, 2023
@Tyrrrz
Copy link
Owner

Tyrrrz commented Jul 3, 2023

@xBaank were you able to use this approach to pull videos that are only accessible as a logged in user? I tried pulling a private video after logging in with my account, but it didn't work 🤔

@xBaank
Copy link
Contributor Author

xBaank commented Jul 3, 2023

I just tested the videos i mentioned in the issue and the download speed wasn't limited, maybe for private videos there are headers missing.
I will take a look.

@Tyrrrz
Copy link
Owner

Tyrrrz commented Jul 3, 2023

I just tested the videos i mentioned in the issue and the download speed wasn't limited, maybe for private videos there are headers missing. I will take a look.

I see. Could be cool to figure out what cookies are needed to get private videos too. Since we're already logged in, might as well.

@xBaank
Copy link
Contributor Author

xBaank commented Jul 3, 2023

It seems like we need to set the cookies that youtube tell us to set each time we make a request to their endpoints or we can't access private videos, and for some reason those cookies are not given at the time we login and and go to www.youtube.com.
Idk if it's better to use a cookie container or replace manually the cookies in the dicctionary with the ones that youtube send us in the headers Set-Cookie.
For now I added a commit that uses a CookieContainer (Its ugly but it works 😅).

@Tyrrrz
Copy link
Owner

Tyrrrz commented Jul 4, 2023

It seems like we need to set the cookies that youtube tell us to set each time we make a request to their endpoints or we can't access private videos, and for some reason those cookies are not given at the time we login and and go to www.youtube.com. Idk if it's better to use a cookie container or replace manually the cookies in the dicctionary with the ones that youtube send us in the headers Set-Cookie. For now I added a commit that uses a CookieContainer (Its ugly but it works 😅).

Can you make a PR with these changes too? 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add login information and store for all downloads
2 participants