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

Implement YouTube OAuth2 login #1670

Closed
wants to merge 20 commits into from
Closed

Implement YouTube OAuth2 login #1670

wants to merge 20 commits into from

Conversation

MichailiK
Copy link
Collaborator

@MichailiK MichailiK commented Aug 24, 2024

This pull request...

  • Fixes a bug
  • Introduces a new feature
  • Improves an existing feature
  • Boosts code quality or performance

Description

This PR adds a toggle to enable the OAuth2 capability found in the YouTube source manager. It also automatically stores & retrieves the refresh token for convenience of JMusicBot users (as the source manager does not do that itself.)

Quick summary of the login flow

Upon enabling OAuth2 (youtubeoauth2=true in the config.txt) the YouTube source manager will log the following:

[13:41:00] [INFO] [YoutubeOauth2Handler]: ==================================================
[13:41:00] [INFO] [YoutubeOauth2Handler]: !!! DO NOT AUTHORISE WITH YOUR MAIN ACCOUNT, USE A BURNER !!!
[13:41:00] [INFO] [YoutubeOauth2Handler]: OAUTH INTEGRATION: To give youtube-source access to your account, go to https://www.google.com/device and enter code ABC-DEF-GHI
[13:41:00] [INFO] [YoutubeOauth2Handler]: !!! DO NOT AUTHORISE WITH YOUR MAIN ACCOUNT, USE A BURNER !!!
[13:41:00] [INFO] [YoutubeOauth2Handler]: ==================================================

Navigating to https://www.google.com/device and entering the code will lead you to an OAuth2 page

OAuth2 page

(the source manager pretends to be a logged in TV client to play tracks)

Once allowed, the source manager logs a refresh token. For convenience, JMusicBot intercepts the log with a turbo filter & stores the refresh token as a file, youtubetoken.txt. The refresh token will be used when JMusicBot gets restarted to retrieve an access token.

Purpose

This should circumvent the "Sign in to confirm you're not a bot" message that people (particularly) on servers have been getting.

Relevant Issue(s)

Fixes #1588

Related PR: #1714

Todo

  • The GUI can't seem to log the messages from the OAuth2 handler, they always end up in stdout. Have to yet look into why

@MichailiK MichailiK mentioned this pull request Aug 24, 2024
@chenchuuu

This comment was marked as off-topic.

@MichailiK

This comment was marked as off-topic.

@chenchuuu

This comment was marked as off-topic.

@djordjepekic

This comment was marked as off-topic.

@joseph23pronton

This comment was marked as off-topic.

@iamayod

This comment was marked as off-topic.

@Tako88

This comment was marked as off-topic.

@jagrosh
Copy link
Owner

jagrosh commented Sep 26, 2024

I'll need to check the logging issue you described. A few concerns:

  • Do these tokens expire? (Do we need to capture the expiration somehow and re-request auth?)
  • Does this log message first appear only when trying to play something, or right when the bot starts? (Should we do a dummy request immediately if this feature is enabled?)
  • Should we have an enumeration so that owners can opt to receive the request via DM instead of in the logs?

@MichailiK
Copy link
Collaborator Author

  • They shouldn't expire by themselves (actual TVs using this stay logged in practically forever from my experience), though one likely could manually revoke them through their Google Account.
  • Right when the bot starts, more specifically when calling yt.useOauth2();
  • I guess we could see if this can be implemented in a neat way

@jagrosh
Copy link
Owner

jagrosh commented Sep 27, 2024

If there is no expiration - so in theory this only happens once - maybe we just do both (log the message, and attempt to DM the owner).

@MichailiK MichailiK linked an issue Oct 1, 2024 that may be closed by this pull request
3 tasks
@MichailiK MichailiK marked this pull request as ready for review October 2, 2024 19:34
@@ -44,6 +45,7 @@ public class Bot
private final PlaylistLoader playlists;
private final NowplayingHandler nowplaying;
private final AloneInVoiceHandler aloneInVoiceHandler;
private final YoutubeOauth2TokenHandler youTubeOauth2TokenHandler;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Honestly a bit of a hacky way of going from the (Lavaplayer) youtube-source logging the details to JDA being ready & DM'ing the owner the details. Feel free to point out better solutions for this!

Copy link
Collaborator Author

@MichailiK MichailiK left a comment

Choose a reason for hiding this comment

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

We probably should (automatically) detect that playback is failing due to the "Sign in to confirm you're not a bot" error and point users to some wiki page on how to sign in

@shweegl

This comment was marked as off-topic.

@MichailiK
Copy link
Collaborator Author

I've made a PR that adds a new page to the wiki: #1714

Ideally we'll wanna merge this PR & the docs PR roughly at the same time, as I'm referring to the new wiki page in here:

LOGGER.error("Track {} has failed to play: {}. You will need to sign in to Google to play YouTube tracks. More info: https://jmusicbot.com/youtube-oauth2", track.getIdentifier(), exception.getMessage());

@OzanKurt

This comment was marked as off-topic.

@MichailiK

This comment was marked as resolved.

@bozoboo

This comment was marked as off-topic.

Repository owner locked as off-topic and limited conversation to collaborators Oct 14, 2024
@MichailiK
Copy link
Collaborator Author

It looks like people are still encountering an error stating "Invalid status code for player api response: 400" that I have to yet look into

@MichailiK
Copy link
Collaborator Author

I'm uncomfortable merging this at the moment, as YouTube seems to have begun making sign in via OAuth2 ineffective.

@MichailiK MichailiK marked this pull request as draft November 12, 2024 10:03
@MichailiK
Copy link
Collaborator Author

Google probably has rolled out a fix for this workaround to everyone by now.

@MichailiK MichailiK closed this Nov 16, 2024
@MichailiK MichailiK deleted the yt-oauth2 branch November 16, 2024 15:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
10 participants